Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda