简体   繁体   中英

Remove entire object from a json array javascript

I have a json array like the picture below在此处输入图像描述

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? 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:

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

Try this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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