I've array
var getData = [
{
"value": "20,
"id": 2
},
{
"value": "30",
"id": 4
},
{
"value": "40",
"id": 6
},
]
And I've data for example I want to check if product_id =2
I want to push ['product_id': 'thisistest'] into getData id = 2
the output will look like this
var getData = [
{
"value": "20,
"id": 2,
"product_id": "thisistest"
},
{
"value": "30",
"id": 4
},
{
"value": "40",
"id": 6
},
]
Here is what I've try
for(let i in getData){
const data = getData[i];
//product_id ==2
if(data.id == product_id) {
getData.push({
//how can I push data into getData where id = 2
});
}
}
"value": "20,it should be"value": "20",, your code will never run, you should have error in console.data['product_id'] = 'thisistest';(inside your if condition) is just enough instead of thatgetData.push, since those items are objects, not arrays. Of course, as long asproduct_idis 2.