我有一个JavaScript对象填充的对象,并且想要删除没有数据的每个对象。这可能是这样的:删除数组中的空对象
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"}
];
我已经使用_.uniq
从underscore.js从我的阵列,它工作正常删除所有重复。虽然它们是唯一的,但当我动态填充数据时(因为有空数据集),总是会留下一个空的对象。我已经尝试_.without
函数,如下所述:Remove empty elements from an array in Javascript但它不起作用。这里是我的尝试:
myArray = _.without(myArray, {id:"",text:""});
数组应该是这样的:
[ {id: "28b", text:"Phill"},
{id: "12c", text:"Peter"},
{id: "43f", text:"Ashley"},
{id: "9a", text:"James"},
];
我也使用jQuery,如果有与此库的解决方案。
拿什么*空*?请添加更多的数据和应该删除的内容。 –
'{id:“”,text:“”}'不是空对象。如果你想删除任何这种发生,过滤它。我想,最后你想要的是删除没有指定'id'的任何对象 –