Home > Back-end >  Deleting an object from an array and changing one of its properties simultaneously - javascript
Deleting an object from an array and changing one of its properties simultaneously - javascript

Time:12-06

I have this array of objects:

const flights = [
    { id: 00, to: "New York", from: "Barcelona", cost: 700, scale: false },
    { id: 01, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true },
    { id: 02, to: "Paris", from: "Barcelona", cost: 210, scale: false },
    { id: 03, to: "Roma", from: "Barcelona", cost: 150, scale: false },
    { id: 04, to: "London", from: "Madrid", cost: 200, scale: false },
    { id: 05, to: "Madrid", from: "Barcelona", cost: 90, scale: false },
    { id: 06, to: "Tokyo", from: "Madrid", cost: 1500, scale: true },
    { id: 07, to: "Shangai", from: "Barcelona", cost: 800, scale: true },
    { id: 08, to: "Sydney", from: "Barcelona", cost: 150, scale: true },
    { id: 09, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false },
  ];

What I want to do is, when I delete one of the objects, for those that come after the deleted object to reduce their id by 1.

For example, if I were to delete the fifth object (id: 04), I want the ids to go in order from 0 to 8 (0, 1, 2, 3, 4, 5, 6, 7, 8) instead of going from 0 to 9 and skipping 4 (0, 1, 2, 3, 5, 6, 7, 8, 9).

This is my code currently:

let flightIdToDelete = 04;

for (let i = 0; i < flights.length; i  ) {
    if (flightIdToDelete === flights[i].id) {
        delete flights[i];
    }
}

/* The array would then become:

const flights = [
    { id: 00, to: "New York", from: "Barcelona", cost: 700, scale: false },
    { id: 01, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true },
    { id: 02, to: "Paris", from: "Barcelona", cost: 210, scale: false },
    { id: 03, to: "Roma", from: "Barcelona", cost: 150, scale: false },
    { id: 05, to: "Madrid", from: "Barcelona", cost: 90, scale: false },
    { id: 06, to: "Tokyo", from: "Madrid", cost: 1500, scale: true },
    { id: 07, to: "Shangai", from: "Barcelona", cost: 800, scale: true },
    { id: 08, to: "Sydney", from: "Barcelona", cost: 150, scale: true },
    { id: 09, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false },
  ]; */

My best guess would be to iterate through the objects' ids starting from the deleted one and then subtracting by 1, but I'm not too sure how to do it.

Help please!!!

CodePudding user response:

You can use the splice method to remove the element from the array and then iterate through the array starting from the index of the deleted element and subtract 1 from the id of each object. i.e. only the elements that are after the removed index will their id be affected.

let flightIdToDelete = 04;

// find the index of the element to delete
const indexToDelete = flights.findIndex(flight => flight.id === flightIdToDelete);

// remove the element from the array
flights.splice(indexToDelete, 1);

// iterate through the array starting from the index of the deleted element
// and subtract 1 from the id of each object
for (let i = indexToDelete; i < flights.length; i  ) {
   flights[i].id -= 1;
}

console.log(flights);

/* The array would now be:

const flights = [
{ id: 00, to: "New York", from: "Barcelona", cost: 700, scale: false },
{ id: 01, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true },
{ id: 02, to: "Paris", from: "Barcelona", cost: 210, scale: false },
{ id: 03, to: "Roma", from: "Barcelona", cost: 150, scale: false },
{ id: 04, to: "Madrid", from: "Barcelona", cost: 90, scale: false },
{ id: 05, to: "Tokyo", from: "Madrid", cost: 1500, scale: true },
{ id: 06, to: "Shangai", from: "Barcelona", cost: 800, scale: true },
{ id: 07, to: "Sydney", from: "Barcelona", cost: 150, scale: true },
{ id: 08, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false },
];
*/

CodePudding user response:

You could just use a filter, and it would be an actual simultaneous change:

const flights = [
    {id: 0, to: "New York", from: "Barcelona", cost: 700, scale: false},
    {id: 1, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true},
    {id: 2, to: "Paris", from: "Barcelona", cost: 210, scale: false},
    {id: 3, to: "Roma", from: "Barcelona", cost: 150, scale: false},
    {id: 4, to: "London", from: "Madrid", cost: 200, scale: false},
    {id: 5, to: "Madrid", from: "Barcelona", cost: 90, scale: false},
    {id: 6, to: "Tokyo", from: "Madrid", cost: 1500, scale: true},
    {id: 7, to: "Shangai", from: "Barcelona", cost: 800, scale: true},
    {id: 8, to: "Sydney", from: "Barcelona", cost: 150, scale: true},
    {id: 9, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false},
];

const id = 4;
let count = 0;

const r = flights.filter((a, idx) => {
    const d = a.id !== id;
    count  = d ? 0 : 1;
    a.id -= count;
    return d;
});

console.log(r);

const flights = [
{id: 0, to: "New York", from: "Barcelona", cost: 700, scale: false},
{id: 1, to: "Los Angeles", from: "Madrid", cost: 1100, scale: true},
{id: 2, to: "Paris", from: "Barcelona", cost: 210, scale: false},
{id: 3, to: "Roma", from: "Barcelona", cost: 150, scale: false},
{id: 4, to: "London", from: "Madrid", cost: 200, scale: false},
{id: 5, to: "Madrid", from: "Barcelona", cost: 90, scale: false},
{id: 6, to: "Tokyo", from: "Madrid", cost: 1500, scale: true},
{id: 7, to: "Shangai", from: "Barcelona", cost: 800, scale: true},
{id: 8, to: "Sydney", from: "Barcelona", cost: 150, scale: true},
{id: 9, to: "Tel-Aviv", from: "Madrid", cost: 150, scale: false},
];

const id = 4;
let count = 0;

const r = flights.filter((a, idx) => {
const d = a.id !== id;
count  = d ? 0 : 1;
a.id -= count;
return d;
});

console.log(r);

  • Related