Skip to content
Advertisement

Find/Replace object in an array and add a key Javascript

I am trying to add a key to an Object of Array as isActive: true, and then I want to find the object in the actual array with the same label as that of selectedFilterList and replace it in this.bindingData else add isActive: false

JavaScript

I have tried this which is working but i don’t think so its a best approach

JavaScript

Advertisement

Answer

You can use Array.prototype.reduce() on data, check if each item in data is present in selectedFilterList using Array.prototype.some() and add isActive flag value accordingly. Here is the sample code:

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