1

seemed like a simple task but im finding it hard to achive. I have object containing child objects, and while looping over them i would like to delete an inner object in based on value of one of it's fields (this field exists in every inner object).

the loop goes like this:

for (let old_item of this.updated_items) {
    if (old_item['content_id'] === item.content_id) {
        this.updated_items.***DELETE***(old_item)
    }
}

I marked location where is the missing logic. How would i delete old_item from this.updated_items? ES6 if possible..thx

4
  • can u please show your html? Commented Aug 9, 2017 at 15:46
  • What do you mean by delete? delete from array / replace by undefined / do a dom deletion ? Commented Aug 9, 2017 at 15:51
  • What exactly is this.updated_items? An array, a Map? Without that information it's impossible to answer the question. All you've shown is that it is iterable. Commented Aug 9, 2017 at 18:24
  • sorry guess i wasn't clear in my title, both typeof()'s of this.updated_items and its content...which are the items, equal to Object Commented Aug 9, 2017 at 20:09

2 Answers 2

3

You can iterate the Object#entries, and when the correct content_id is found on the value, delete the key from the original object.

for (const [key, value] of Object.entries(this.updated_items)) {
    if (value.content_id === item.content_id) {
        delete this.updated_items[key];
    }
}

Since Object#entries is not supported by some browsers, another option is to use Array#forEach to iterate the object's keys, and if the content_id is found delete the key from the original object:

Object.keys(this.updated_items) // get the object's keys
    .forEach((key) => // iterate the keys
                this.updated_items[key].content_id === item.content_id // if the content id is similar
                && 
                delete this.updated_items[key] //delete the key from the original object
     )
Sign up to request clarification or add additional context in comments.

5 Comments

I'm not sure using delete keyword is what OP meant
As far as I understand it, he wants to remove a property from an object, and delete is the way to do so. If he thinks otherwise, he'll select another answer :)
You also don't need to use Object.entries but just a for...of should do the trick (if browser support it)
For of needs an iterable and object is not an iterable.
Yeah I just tried it and saw my mistake... However I saw somewhere that there is another than object.entries that is better on MDN. Let me look at it and come back to you if found :)
1

You can use the filter function over an array, and convert your object to an array using Array.from:

this.update_items = Array.from(this.updated_items).fiter(old_item => old_item.content_id !== item.content_id)

3 Comments

It's not an array, it's an object containing child objects (I posted more or less the same answer before realizing this)
Oh ok, Maybe then using Array.from would solve this issue
I don't know OPs case, but it works with a list of node elements (for instance by calling myOl.children)

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.