0

If I have 6 objects in my array, and 4 of these objects have a parentId key:value

myArray[person{}, person{}, person{}, person{}, person{}, person{}]

person{
    name: 'XXX',
    age: 'XXX',
    parentId: 'XXX'
}

Can I select the 2 which have no parentId key:value

person{
    name: 'XXX',
    age: 'XXX'
}

And then put them into a new array?

1 Answer 1

2

Yes, using Array.prototype.filter

var newArrWithoutParentId = myArray.filter(function(x){
    return !x.hasOwnProperty("parentId");
});
Sign up to request clarification or add additional context in comments.

2 Comments

nice and concise, I like it. Just be aware the filter() function is only available in IE9+
@Daft glad to have helped you out! :)

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.