Skip to content
Advertisement

How to add “new” element/objects s to a list in javascript?

Let’s say I have a List of objects in javascript.

JavaScript

Now I make some API call and get more data like …

JavaScript

I want to add the new objects i.e the sixth and seventh to my existing demoList and discard the existing object i.e. first object. How do I achieve this in javascript with less time complexity? No I cannot use sets. Also let’s just say the criteria for comparison is the name. Other values can be same.

Advertisement

Answer

We can check the result by Array.prototype.find() method and then push it.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement