简体   繁体   English

从 json 阵列 javascript 中删除整个 object

[英]Remove entire object from a json array javascript

I have a json array like the picture below我有一个 json 阵列,如下图所示在此处输入图像描述

The position for marker 1 is Null, how can i remove the entire marker 1 object from the array so i only have marker 2,3 and 4 left in the array?标记 1 的 position 是 Null,如何从数组中删除整个标记 1 object,所以数组中只剩下标记 2、3 和 4? thank you谢谢你

jsonArray = jsonArray.filter(item => !item.position.includes(null))

Or或者

jsonArray = jsonArray.filter(item => item.position[0] !== null && item.position[1] !== null)

You could do something like:您可以执行以下操作:

const result = arrayOfObjects.filter((object) => {
  return object.position[0] !== null && object.position[1] !== null);
}

We can use a library lodash and using filter method, like this:我们可以使用库 lodash 并使用过滤器方法,如下所示:

var items = _.filter(items, function(item){return item.position});

or或者

var items = _.filter(items, function(item){return item.position !== null && item.position !== ""});

we can filter out objects which do have position null我们可以过滤掉具有 position null 的对象

Try this code:试试这个代码:

const filtered = array.filter(items => items.position[0] != null || items.position[1] != null);
console.log(filtered);

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

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