0

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

[ [], [] ]
2
  • Not that this is the main issue, but consider using filter instead? Commented Oct 30, 2020 at 21:53
  • Your if condition is wrong, you need to use el.id>=2000 Commented Oct 30, 2020 at 23:35

2 Answers 2

2

I guess the line if(obList.id >= 2000){ should be if(el.id >= 2000){ as obListdoesn't have any id field.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to use el.id instead of obList.id.

obList.id is always undefined because it points to your array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.