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?

3
  • 2
    object provided is invalid Commented Feb 17, 2017 at 15:01
  • @all wrong duplicate. Its not woking because its an object in an array in an array in an object. Has nothing todo with the answer provided. Commented Feb 17, 2017 at 15:04
  • @Zakaria Acharki It's not a duplicate... OP issue is different than that inside the link you've provided. Commented Feb 17, 2017 at 15:08

3 Answers 3

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);

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

5 Comments

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!
@AndreiGheorghiu He miss one square bracket [ just right after cards: and index of element [0] after [key] (which is wrong).
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 Stack Overflow hoping some kind user will refactor it. You get my point?
@AndreiGheorghiu I would edit my answer and describe everything, but you described it beautifully already. :)
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.
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.

Comments

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];
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.