0

I have an array like this one:

[{
  '0': [7]
}, {
  references: [0]
}, {
  '1': [8]
}, {
  references: [9]
}, {
  '2': [20]
}, {
  references: [50]
}]

I want to remove all objects containing the reference key. Any suggestions?

2

2 Answers 2

4

not pop or splice, filter

var a = [
           { 0:[7]}, 
           { references:[0]}, 
           { 1:[8]}, 
           {references:[9]}, 
           { 2:[20]}, 
           { references:[50]}
];

var filteredA = a.filter(function(item){ return !('references' in item) });
Sign up to request clarification or add additional context in comments.

Comments

0

Use object in literal notation. Then you can find the key by hasOwnProperty method.

var a = [
           { 0:[7]}, 
           { references:[0]}, 
           { 1:[8]}, 
           {references:[9]}, 
           { 2:[20]}, 
           { references:[50]}
]
for(i=0; i<a.length; i++) { 
    if(a[i].hasOwnProperty('references')) { 
        a.splice(i, 1);
    } 
}

Comments

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.