2

i have ab object like this:

{cards: [{id: 1}], [{id: 2}],[{id: 2}],[{id: 3}],[{id: 4}] ]

i want to delete, for instance, id:3 from object and tried this:

        for (let key in _tempCards.cards) {

            if (_tempCards.cards[key].id === 3)
                delete _tempCards.cards[key];

    }

object should look like this

{cards: [{id: 1}], [{id: 2}],[{id: 2}],[{id: 4}] ]

how to delete this part of the object?

asdfboy
  • 59
  • 4

3 Answers3

3

Had to refactor your object a little bit to make it work.

var _tempCards = {
  cards: [
    [{
      id: 1
    }],
    [{
      id: 2
    }],
    [{
      id: 3
    }],
    [{
      id: 4
    }]
  ]
}

for (var i = 0; i < _tempCards.cards.length; i++) {
  if (_tempCards.cards[i][0].id == 3) {
    _tempCards.cards.splice(i, 1);
  }
}

console.log(_tempCards);
kind user
  • 32,209
  • 6
  • 49
  • 63
  • It's clear his data object was broken. So you had to fix it by choosing a particular data structure which seemed probable. However, you're not explaining what you did and tomorrow they'll likely make the same mistake, not knowing where they are wrong, trying *"your fix"* and... guess what? It won't work. It is preferred you explain where they were wrong and how they could improve their approach/method in order to not make the same mistake again. I hope you agree. Cheers! – tao Feb 17 '17 at 16:24
  • @AndreiGheorghiu He miss one square bracket `[` just right after `cards:` and index of element `[0]` after `[key]` (which is wrong). – kind user Feb 17 '17 at 16:33
  • I believe you're supposed to tell them that. You know what they will learn from your answer? Instead of learning how to validate their data, they will learn they need to put the object on [so] hoping some kind user will refactor it. You get my point? – tao Feb 17 '17 at 16:37
  • @AndreiGheorghiu I would edit my answer and describe everything, but you described it beautifully already. :) – kind user Feb 17 '17 at 16:38
  • 1
    It's ok, My point was to make you understand and improve your future answers, not this one in particular. Take care and remember to have fun. – tao Feb 17 '17 at 16:38
1

The code you provided is not an object, no matter how you look at it.

{cards: [{id: 1}], [{id: 2}],[{id: 2}],[{id: 3}],[{id: 4}] ]

Let's break it down:

  1. You open an object, but never close it. Fine, let's close it...
    {cards: [{id: 1}], [{id: 2}],[{id: 2}],[{id: 3}],[{id: 4}] ]}

  2. Now you (kind of) have an object and the first property (cards) contains an array with a single object: [{id:1}], than you have a bunch of other properties without keys, which are on the same level with cards, they are not inside the cards property of your parent object, as I believe you intended...

  3. ... and you're also closing an array after your last keyless property, which I have no idea where you started (/ intended to start but forgot), so I had to remove that to in order to get a valid object.

My best guess at what your object was supposed to look like is:

{ cards: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ] }

or, beautified...

Object = {
  cards: [{
    id: 1
  },
  {
    id: 2
  },
  {
    id: 3
  },
  {
    id: 4
  }]
}

You now have an array of objects in the cards property of Object. Another probable data structure would be

cards: [ [{ id: 1 }], [{ id: 2 }], [{ id: 3 }], [{ id: 4 }] ]

This is the structure @KindUser believed to be most probable for your data.


The conclusion:, it is quite clear your object is "man made". And it's erroneous. You either use a tool to validate and visualize your data (search for "JSON viewer", "beautify js", etc...) or you pay more attention to what you're doing.

tao
  • 59,850
  • 12
  • 84
  • 110
0
_tempCards.cards=_tempCards.cards.filter(function(arr){
    return arr[0].id!==3;//allow every arrays first element, that hasnt an id of 3
});

You could use the cool Array.prototype.filter, to filter your array. Your code basically work, but you have to take into consideration, that your array contains arrays containing your objects.

for (let key in _tempCards.cards) {
    if (_tempCards.cards[key][0].id === 3)
            delete _tempCards.cards[key];
    }
}
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120