0

I have a sample script

const building_arr = [
    { id: 1, name: 'Test 1', city_id: 5 },
    { id: 1, name: 'Test 2', city_id: 1 },
    { id: 1, name: 'Test 3', city_id: 1 },
    { id: 1, name: 'Test 4', city_id: 3 },
    { id: 1, name: 'Test 5', city_id: 5 },
];
let building_object = {};
building_object = building_arr.filter(function (building) {
    if (building.id == 1) return building;
});

I want get first object {id:1,name:"Test 2",city_id:1}, but result not work

DecPK
  • 24,537
  • 6
  • 26
  • 42
Hai Truong IT
  • 4,126
  • 13
  • 55
  • 102

1 Answers1

-1

You need to use find rather than filter. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Alex
  • 848
  • 6
  • 7