我在這裏做錯了什麼?Javascript:過濾數組對象
var locations = [
{ id: 1, name: 'N'},
{ id: 2, name: 'P'}
]
var employee = { location_id: 1 }
locations.filter((location) => {
return location.id == employee.location_id
});
console.log(locations);
這個返回undefined
當我試圖使它返回{ id: 1, name: 'N'}
。
此代碼適用於我。 –
它實際上返回一個包含該對象的數組。嘗試使用'Array.prototype.findIndex'來獲取數組中對象的索引。 – lukaleli
這段代碼當然不會以任何方式產生'undefined'。所以,如果你明白了,你一定在做着非常不同的事情。 –