0

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?

2
  • Try this.myArray.find(x => x.id === id)[0].data.find(x => x.dataId === dataId)[0].active = activeValue. Commented Feb 20, 2022 at 15:45
  • .data[dataId] -> you have an array of objects, you cannot just use the index. Moreover you already know how to lookup something by a property. You already have the code for finding an item by its id. Do the same but for dataId instead. Commented Feb 20, 2022 at 15:46

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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.