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]}
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.
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 }
]