JavaScript Remove empty Objects from Array remove empty objects from json,remove empty objects from array

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.

Answer:1

// 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>
Answer:2

You can try this:

_.filter(myArray, _.isEmpty)

I assume empty means

var obj = {}
Answer:3

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);
Answer:4

In local storage I have an object named favourites and it contains this.. "{ "id3333":{ "URL":"somewhere.comm/page1/", "TITLE":"Page 1 Title", }, "id4444":{ "URL":"...

In local storage I have an object named favourites and it contains this.. "{ "id3333":{ "URL":"somewhere.comm/page1/", "TITLE":"Page 1 Title", }, "id4444":{ "URL":"...

  1. c++ delete object within itself
  2. javascript delete key within object

I have a resources array which is full of objects. Each object has categories array full of objects. I am trying to apply a filter to only return resources that have category objects of a specific ...

I have a resources array which is full of objects. Each object has categories array full of objects. I am trying to apply a filter to only return resources that have category objects of a specific ...

How do I set up CORS on Amazon S3 to allow only approved domains to access a JS script in my S3 bucket? Currently, I have CORS set as follows on my S3 bucket: <?xml version="1.0" encoding="UTF-8"?&...

How do I set up CORS on Amazon S3 to allow only approved domains to access a JS script in my S3 bucket? Currently, I have CORS set as follows on my S3 bucket: <?xml version="1.0" encoding="UTF-8"?&...

  1. restrict access using permission set
  2. restrict access using xhost
  3. restrict access using htaccess
  4. restrict access using sql
  5. restrict access using iis
  6. restrict network access using firewall-cmd/firewall
  7. restrict internet access using group policy
  8. restrict internet access using active directory
  9. restrict ec2 access using tags
  10. restrict folder access using htaccess
  11. restrict network access using firewall-cmd/firewall rhcsa
  12. restrict internet access using dns
  13. restrict internet access using hosts file
  14. restrict access to websites using group policy
  15. restrict access to web page using web.config

So basically I am using a jquery plug-in called blueprint split layout. The code for it can be found at: http://tympanus.net/Blueprints/SplitLayout/index.html. I modified the code to fit with my ...

So basically I am using a jquery plug-in called blueprint split layout. The code for it can be found at: http://tympanus.net/Blueprints/SplitLayout/index.html. I modified the code to fit with my ...