Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda