Q:

Filter array items using forEach

I have one question about filter array in forEach. So I would like filter (bigger than in example) array using outside variable filterKey. I think that my function is correct by after filtered newArr is undefined. Could you explain what is incorrect?
var filterKey = 123456,
var array = [{ 
              ratings:{ users:[id: 123456]}, user: xyz
             },
             {
              ratings:{users:[id:9787389023]}, user:zyx
            }],

And my filter function
var newArr = array.forEach((ele) =>
                ele.ratings.users.filter((newEl) =>
                    newEl.id == filterKey))

A:

Use array.filter method

let array = [
{
  id: 123456, user: 'xyz'
},
{
  id:9787389023, user: 'zyx'
},
{
  id: 123456, user: 'che'
}
]

let newArray = array.filter((element) => element.id === 123456)
console.log(newArray)

