Deleting a row from a JavaScript object if it matches with all properties
Solution 1:
Don't even specify the properties as arguments. You can define them as an object
itself.
This would also work
airport_data_1 = [
{ departure_time: "12:00", arrival_time: "03:00", city_id: "BOS" },
{ departure_time: "12:00", arrival_time: "03:00", city_id: "BOS" },
{ departure_time: "01:00", arrival_time: "04:00", city_id: "SFO" },
{ departure_time: "03:00", arrival_time: "05:00", city_id: "BOS" },
{ departure_time: "03:00", arrival_time: "05:00", city_id: "SFO" },
{ departure_time: "04:00", arrival_time: "06:00", city_id: "SJC" },
{ departure_time: "04:00", arrival_time: "06:00", city_id: "JFK" },
{ departure_time: "06:00", arrival_time: "09:00", city_id: "SJC" },
];
function remove_airport_row(arr, obj) {
return arr.filter((row) => {
// ingore the row if all the the properties matches to obj
return !Object.entries(obj).every(([key, value]) => row[key] === value);
})
}
console.log(remove_airport_row(airport_data_1, {
departure_time: "06:00",
arrival_time: "09:00",
city_id: "SJC",
}));
Refer the docs: Object.entries, Array.prototype.every, Array.prototype.filter if you are not familiar with them.