Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Updating attribute value inside array

I have a array as follows:

 myArray =  [
        {
          "id": 1,
          "data": [
            {
              "active":1,
              "dataId":1
            },
            {
               "active":0,
               "dataId":2
            },
            {
            {
               "active":1,
               "dataId":3
            }
            }
          ]
        }
    
    ]

Above is just sample array, at run time it can have many elements. I have a method as follows which receives id, dataId and active(value of active can be 0 or 1) as argument.

myMethod(id, dataId, activeValue) {

}

When this Id and dataId is received, I need find element with that id, then go inside data array and find element with dataId and set value of active attribute with the active value received in argument.

My try:

 myMethod(id, dataId, activeValue) {
   this.myArray.find(item => item.id === id).data[dataId].active = activeValue
}

But this code is not updating value. How can I do that?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Close! Your mistake lies in the part to update the active property, the dataID does not match the index of the array.

You should replace .data[dataId] with .data.find(dataItem => dataItem.dataId === dataId)

This should work:

function myMethod(id, dataId, activeValue) {
   myArray.find(item => item.id === id).data.find(dataItem => dataItem.dataId === dataId).active = activeValue
}
about 4 years ago · Juan Pablo Isaza Report

0

First, your object has unnecessary {} around the last .data[] element. Then you have to define the function as a method of the object in order for this to point to the object:

const myArray =  [
        {
          "id": 1,
          "data": [
            {
              "active":1,
              "dataId":1
            },
            {
               "active":0,
               "dataId":2
            },
            {
               "active":1,
               "dataId":3
            }
          ]
        } 
    ];
    
  
myArray.myMethod = function(id, dataId, activeValue) {
   this.find(item => item.id === id).data.find(d => d.dataId === dataId).active = activeValue;
}

myArray.myMethod(1,3,0);

console.log( myArray );
//LAST data[] element: "active":1 ===> "active":0

about 4 years ago · Juan Pablo Isaza Report

0

Test data:

myArray = [
{
    "id": 1,
    "data": [
        {"active": 1, "dataId": 1},
        {"active": 0, "dataId": 2},
        {"active": 1, "dataId": 3},
        {"active": 1, "dataId": 4},
        {"active": 1, "dataId": 5},
        {"active": 1, "dataId": 6}
    ]
},
{
    "id": 2,
    "data": [
        {"active": 1, "dataId": 1},
        {"active": 0, "dataId": 2},
        {"active": 1, "dataId": 3},
        {"active": 1, "dataId": 4},
        {"active": 1, "dataId": 5},
        {"active": 1, "dataId": 6}
    ]
},
{
    "id": 3,
    "data": [
        {"active": 1, "dataId": 1},
        {"active": 0, "dataId": 2},
        {"active": 1, "dataId": 3},
        {"active": 1, "dataId": 4},
        {"active": 1, "dataId": 5},
        {"active": 1, "dataId": 6}
    ]
}];

Array method:

myArray.myMethod = function(id, dataId, activeValue) {
this.find(item => item.id === id)
    .data.find(item => item.dataId === dataId)
    .active = activeValue;
}

Method call and test log:

myArray.myMethod(3, 6, 0);
console.log(...myArray);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!