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.
No need for a library, just take
Array#filter
and an object. With dynamic filtering, for all properties.You can try this:
I assume empty means
try (ECMA5+):