Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I have a JavaScript-array with objects filled in and want to remove every object with no data. It might look like this:

var myArray = [ {id: "28b", text:"Phill"},
                {id: "12c", text:"Peter"},
                {id: "43f", text:"Ashley"},
                {id: "43f", text:"Ashley"},
                {id: "", text:""},
                {id: "9a", text:"James"},
                {id: "", text:""},
                {id: "28b", text:"Phill"}
              ];

I already use _.uniq from underscore.js to remove all duplicates from my array, which works fine. Though they are unique, one empty Object is always left when I dynamically fill in data (because there are empty datasets). I already tried the _.without function as mentioned here: Remove empty elements from an array in Javascript but it doesn't work. Here is my attempt:

myArray = _.without(myArray, {id:"",text:""});

The array should look like this:

              [ {id: "28b", text:"Phill"},
                {id: "12c", text:"Peter"},
                {id: "43f", text:"Ashley"},
                {id: "9a", text:"James"},
              ];

I am also using jQuery if there is a solution with this library.

share|improve this question
1  
what means empty? please add some more data and what should be removed. – Nina Scholz Jul 22 at 8:28
    
{id:"",text:""} isn't empty object. If you want to remove any of this occurence, filter it. And i guess, finally what you want is to remove any object that has no id specified – A. Wolff Jul 22 at 8:28
up vote 0 down vote accepted

// Code goes here

myArray = [{
    id: "28b",
    text: "Phill"
  }, {
    id: "12c",
    text: "Peter"
  }, {
    id: "43f",
    text: "Ashley"
  }, {
    id: "43f",
    text: "Ashley"
  }, {
    id: "",
    text: ""
  }, {
    id: "9a",
    text: "James"
  }, {
    id: "",
    text: ""
  }, {
    id: "28b",
    text: "Phill"
  }

]

var result = _.filter(_.uniq(myArray, function(item, key, a) {
  return item.id;
}), function(element) {
  return element.id && element.text
});
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

share|improve this answer
    
works flawlessly, thank you! :) – Jandroide Jul 22 at 8:52

You can try this:

_.filter(myArray, _.isEmpty)

I assume empty means

var obj = {}
share|improve this answer
    
would it be safer to do !obj || _.isEmpty(obj) – Callum Linington Jul 22 at 8:29
    
I like more declarative approach - matter of taste I think – Oskar Szura Jul 22 at 8:30
    
I just meant, like that would only remove empty? would it also remove null or undefined? – Callum Linington Jul 22 at 8:30
    
Apparently so – Callum Linington Jul 22 at 8:32
    
lodash.com/docs#isEmpty - _.isEmpty(undefined) = true, _.isEmpty(null) = true - I think it's sufficient – Oskar Szura Jul 22 at 8:32

If you want to mutate the array then lodash provides the remove function:

_.remove(myArray, {id:"",text:""});

or using underscore's reject function which doesn't mutate the original collection:

myArray = _.reject(myArray, {id:"",text:""});
share|improve this answer
    
both are working very simple, thanks :) – Jandroide Jul 22 at 9:24

No need for a library, just take Array#filter and an object. With dynamic filtering, for all properties.

var myArray = [{ id: "28b", text: "Phill" }, { id: "12c", text: "Peter" }, { id: "43f", text: "Ashley" }, { id: "43f", text: "Ashley" }, { id: "", text: "" }, { id: "9a", text: "James" }, { id: "", text: "" }, { id: "28b", text: "Phill" }],
    filtered = myArray.filter(function (a) {
        var temp = Object.keys(a).map(function (k) { return a[k]; }),
            k = temp.join('|');

        if (!this[k] && temp.join('')) {
            this[k] = true;
            return true;
        }
    }, Object.create(null));

console.log(filtered);

share|improve this answer
    
Pure js is an even better solution than with underscore.js, thanks for that! Is there also a short filter function to remove all duplicates, so that I don't have to use underscore.js - functions at all? – Jandroide Jul 22 at 9:08
    
@Jandroide, please see edit. – Nina Scholz Jul 22 at 9:27
    
I really appreciate your help but the duplicates are still remaining – Jandroide Jul 22 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.