Reputation: 21
I am have a problem that I am not able to push the object into the nested array. Please help thank you!
Here is my code:
let obList = [
{ date: '12/1/2011', reading: 3, id: 2055 },
{ date: '13/1/2011', reading: 5, id: 2053 },
{ date: '14/1/2011', reading: 6, id: 1652 },
{ date: '14/1/2011', reading: 6, id: 152 },
{ date: '14/1/2011', reading: 6, id: 1562 },
{ date: '14/1/2011', reading: 6, id: 2662 },
{ date: '14/1/2011', reading: 6, id: 3652 }
];
let lit = ["Saab", "Volvo", "BMW"];
let mainList = [[],[]]
for (let i = 0; i < 2; i++){
obList.forEach(el=>{
console.log(el.id >= 2000)
if(obList.id >= 2000){
mainList[i].push("ss")
}
})
}
console.log(mainList)
The output for the mainList is empty
[ [], [] ]
Upvotes: 0
Views: 77
Reputation: 1378
You have to use el.id
instead of obList.id
.
obList.id
is always undefined because it points to your array.
Upvotes: 2
Reputation: 905
I guess the line if(obList.id >= 2000){
should be if(el.id >= 2000){
as obList
doesn't have any id field.
Upvotes: 2