简体   繁体   English

从数组中删除空对象

[英]Remove empty Objects from Array

I have a JavaScript-array with objects filled in and want to remove every object with no data. 我有一个包含对象的JavaScript数组,并且想要删除没有数据的每个对象。 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. 我已经在underscore.js中使用_.uniq从我的数组中删除了所有重复项,效果很好。 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. 我已经尝试过_.without函数,如此处所述: 从Javascript数组中删除空元素,但不起作用。 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. 如果此库有解决方案,我还将使用jQuery。

You can try this: 您可以尝试以下方法:

_.filter(myArray, _.isEmpty)

I assume empty means 我认为是空的意思

var obj = {}

 // 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> 

No need for a library, just take Array#filter and an object. 不需要库,只需使用Array#filter和一个对象。 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); 

try (ECMA5+): 试试(ECMA5 +):

var myArrayFiltered = myArray.filter((ele) => {
  return ele.constructor === Object && Object.keys(ele).length > 0
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM