1

I am trying to delete a row from an object if that row has the same values as the property I want to delete.

This is my attempt and it works, just wondering if there is a more efficient way

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(obj, prop1, prop2, prop3) {
    var i = obj.length;
    if (i) {   // (not 0)
        while (--i) {
        var current = obj[i];
            if (current.departure_time == prop1 && current.arrival_time == prop2 && current.city_id == prop3) {
                obj.splice(i, 1);
            }
        }
    }
}

remove_airport_row(airport_data_1, "06:00","09:00","SJC");
console.log(JSON.stringify(airport_data_1));

Desired result

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"}];
Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
bombombs
  • 593
  • 1
  • 13
  • This code seems to work as expected? What issue are you facing? do you get any errors? --- Edit: Oh sorry, you're asking for improvement. – evolutionxbox Jan 21 '22 at 14:50

4 Answers4

3

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.

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
0

You can use the filter methods of arrays:

const filteredArray = airport_data_1.filter(airport => {
  return !(
     airport.departure_time === "06:00" &&
     airport. arrival_time "09:00" &&
     airport.city_id === "SJC"
  );
});

In this way you'll have new array (filteredArray) that will contains all the elements of airport_data_1 excluded the one with the data you selected. If you prefer to remove it directly from the original array instead of creating a purged copy you can use the findIndex method:

const removeId = airport_data_1.findIndex(airport => {
  return (
     airport.departure_time === "06:00" &&
     airport. arrival_time "09:00" &&
     airport.city_id === "SJC"
  );
});

airport_data_1.splice(removeId, 1);
Raffaele
  • 737
  • 7
  • 21
0

You can you .filter() to remove all elements where all three props are equal:

const 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(obj, prop1, prop2, prop3) {
    return obj.filter(el => el.departure_time !== prop1 || el.arrival_time !== prop2 || el.city_id !== prop3);
}


console.log(remove_airport_row(airport_data_1, "06:00","09:00","SJC"));
Xeelley
  • 1,081
  • 2
  • 8
  • 18
0
You can put filter in function if you want it as a function()
Filter returns all objects that there props do not match the passed object. You can add props as params or the whole object as i did in my code which i prefer

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"},
{"departure_time" :"06:00","arrival_time":"09:00","city_id":"SJC"}];

const testData = {"departure_time" :"06:00","arrival_time":"09:00","city_id":"SJC"}
console.log("length before filtering data = " ,airport_data_1.length);
let filtered_data = airport_data_1.filter(e => {
    // removes all rows that match with testData  == the last two objects in airport_data_1
    // added one extra obj in index 8 (line 10) to test if the function removes two or more objects with the same value
    return !(e.arrival_time === testData.arrival_time && e.departure_time === testData.departure_time && e.city_id === testData.city_id)
})
console.log(filtered_data)
console.log("length after filtering data = " ,filtered_data.length)