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

182
Visualizações
How to use specific index in Map function es6?

I have map function and i know the index also instead of iterating all the values i need only one particular value in that map how do i do that ?

 Result.map(function(product,index) { 
             
            product.data.map(function(attribute){
                    
                    if (attribute.id == fieldName) 
                    {
                      console.log('details',detail);
                      
                      attribute.Qty= 1;
                    
              
            });
        });

i have tried something like this

  const list1 = Result.map(e => e.data[currIndex].Qty)[rowIndex];

and my mock data is

0: {data: Array(6)}
1: {data: Array(10)}
2: {data: Array(7)}


data: Array(6)
0:
{
 id: 001
 Qty: 1
},
{
 id: 002
 Qty: 2
},
{
 id: 003
 Qty: 3
},
{
 id: 004
 Qty: 4
},
{
 id: 005
 Qty: 5
},
{
 id: 006
 Qty: 6
}

and i need to get something like matching id : 005 need to update the Qty to 5

0: {data: [id: 005,Qty: 5]}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I got an invalid data structure error and an octal prefix error when I reproduced your use case. I fixed it up a bit to illustrate how you can match your id and change the value of Qty You can do this quite simply using map.

Array.prototype.map()

Parameters

callbackFn - Function that is called for every element of arr. Each time callbackFn executes, the returned value is added to newArray.

The callbackFn function accepts the following arguments:

element - The current element being processed in the array.

indexOptional - The index of the current element being processed in the array.

arrayOptional - The array map was called upon.

thisArgOptional - Value to use as this when executing callbackFn.

Example

let data = [
    {id: 0o01, Qty: 1},
    {id: 0o02, Qty: 2},
    {id: 0o03, Qty: 3},
    {id: 0o04, Qty: 4},
    {id: 0o05, Qty: 5}
];

const editData = (myData, id, newValue) =>
    myData.map((obj, index) => {
        console.log('index value: ', index)
        if (obj.id === id) obj.Qty = newValue
    })

editData(data, 0o05, 555)
console.log(data)

Output

index value:  0
index value:  1
index value:  2
index value:  3
index value:  4
[
  { id: 1, Qty: 1 },
  { id: 2, Qty: 2 },
  { id: 3, Qty: 3 },
  { id: 4, Qty: 4 },
  { id: 5, Qty: 555 }
]
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