Home > database >  Updating attribute value inside array
Updating attribute value inside array

Time:02-21

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?

CodePudding user response:

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
}

CodePudding user response:

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

CodePudding user response:

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

  • Related