1

I have a json array like the picture below enter image description here

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

4 Answers 4

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

Or

jsonArray = jsonArray.filter(item => item.position[0] !== null && item.position[1] !== null)
Sign up to request clarification or add additional context in comments.

6 Comments

hi i know this works, but i need it to be filtered based on condition, position = null
@Aaron Well position is a list that contains null values, but it's not null itself. So its content needs to be checked.
yup so do you have a solution for this, that's my actual problem
This works for OP's requirement, but will also remove positions on the Null Island, so jsonArray = jsonArray.filter(item => item.position[0] !== null && item.position[1] !== null) to be precise.
@Aaron It's not possible to check if a list that has values is null, or even an empty list. I mean you can check but the result is false. [] === null would always return false. The closest you can get is check whether that list includes null which I've updated my answer to show that too.
|
0

You could do something like:

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

Comments

0

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

Comments

-1

Try this code:

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

1 Comment

@Aaron see the answer. I have updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.