0

I have below structure:

var value = 4;

Object value look like below image (console.log)

enter image description here

I want to delete entire row when value matches player_id. I am using below, but look like it is not able to delete entire row (as I can see '0: Object' even after deletion.

for (var key in obj) {
    if (obj[key].player_id === value) {
        delete obj[key];
        break;
    }
}
6
  • 1
    please add the object in text notation (object literal), not as image. Commented Oct 6, 2016 at 6:25
  • Just FYI, your obj is an array. Use splice(index, 1) Commented Oct 6, 2016 at 6:26
  • you can use unshift() method Commented Oct 6, 2016 at 6:31
  • obj.indexOf(value) returns -1 always. Commented Oct 6, 2016 at 6:32
  • @NiteshKumar indexOf will return first matching value. If you are on ES6, you can use array.findIndex() Commented Oct 6, 2016 at 6:34

2 Answers 2

3

When you use delete on array element, it is replaced by undefined. Correct way to delete element from array is to use array.splice()

Delete

var obj = [{
  player_id: 1,
  team_id: 2
}, {
  player_id: 2,
  team_id: 2
}, {
  player_id: 3,
  team_id: 1
}, {
  player_id: 4,
  team_id: 1
}]

var value = 4;

for (var key in obj) {
    if (obj[key].player_id === value) {
        delete obj[key];
        break;
    }
}
console.log(obj)

Array.splice

var obj = [{
  player_id: 1,
  team_id: 2
}, {
  player_id: 2,
  team_id: 2
}, {
  player_id: 3,
  team_id: 1
}, {
  player_id: 4,
  team_id: 1
}]

var value = 4;

for (var i =0; i< obj.length; i++) {
    if (obj[i].player_id === value) {
        obj.splice(i,1)
        break;
    }
}
console.log(obj)

Also, for..in is for objects. You should use for or any other array function to loop over arrays.

Array.findIndex

var obj = [{
  player_id: 1,
  team_id: 2
}, {
  player_id: 2,
  team_id: 2
}, {
  player_id: 3,
  team_id: 1
}, {
  player_id: 4,
  team_id: 1
}]

var value = 4;

var index = obj.findIndex(x=>x.player_id === value);
obj.splice(index,1)
console.log(obj)

Note: array.findIndex is a part of ES6 and will have compatibility issue. Please refer them before using.

Sign up to request clarification or add additional context in comments.

1 Comment

obj.splice worked for me. Thank you very much Rajesh.
1

I think this is what you needed

var value = 4;

var arr = [
  {player_id: '11', team_id: '1'},
  {player_id: '4', team_id: '2'},
  {player_id: '10', team_id: '1'}
];


arr = arr.filter(function(item){
    return item.player_id != value;
})

console.log(arr)

Hope this helps :)

2 Comments

Just a pointer, filter is more suited when you want another array. Not assigning and not returning will make it similar to forEach and you should use it.
@Mr.7, that's an awful way to approach that, not only abusing filter as forEach, but also a (potentially) repeated mutation of the Array you're iterating (and the iterator itself has no clue of that), and all that just to generate a subset. use filter like this: var subset = arr.filter(function(item){ return item.player_id !== "4" }). and if the asker really wants to replace the original Array: arr = arr.filter(...)

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.