Home > Mobile >  How to delete an array element from an object ? - Javascript
How to delete an array element from an object ? - Javascript

Time:09-16

I have this example :

var ex = {
  "list_1": {
    "money": 1000,
    "manager": "Louis",
    "cars": ["mazda", "ford_focus"]
  },
  "list_2": {
    "money": 300,
    "manager": "Keen",
    "cars": ["fiat", "dacia"]
  }
};

function updateFunction(option, id, prop, value) {
  if (option == "update") {
    if (prop == "money" && value != "") {
      ex[id][prop] = value;
    } else if (prop == "manager" && value != "") {
      ex[id][prop] = value;
    } else if (prop == "cars") {
      ex[id][prop].push(value);
    }
  }

  return ex;
};

updateFunction("update", "list_1", "manager", "Andrew");
console.log(ex)

Now I want to include in the above function something to delete the first element from the cars array example : updateFunction("delete","list_1","cars"......)

Result wanted : cars[ford_focus] What should I do ? I tried some ideas from the net , but nothing doesn't work.

CodePudding user response:

You can delete the first element of an array using shift as

ex[id][prop].shift()

You can add custom validation according to your requirement. I've just added

if (prop === "cars")

because I know I'm dealing with an Array and I can use shift on an array object only

var ex = {
  list_1: {
    money: 1000,
    manager: "Louis",
    cars: ["mazda", "ford_focus"],
  },
  list_2: {
    money: 300,
    manager: "Keen",
    cars: ["fiat", "dacia"],
  },
};

function updateFunction(option, id, prop, value) {
  if (option == "update") {
    if (prop == "money" && value != "") {
      ex[id][prop] = value;
    } else if (prop == "manager" && value != "") {
      ex[id][prop] = value;
    } else if (prop == "cars") {
      ex[id][prop].push(value);
    }
  } else if (option === "delete") {
    if (prop === "cars") ex[id][prop].shift();
  }
  return ex;
}

//updateFunction("update", "list_1", "manager", "Andrew");
console.log(updateFunction("delete", "list_1", "cars"));
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */

.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

CodePudding user response:

You can use a filter() call and pass an array of indexes to filter against. This could easily be adapted to also delete by value.

function updateFunction(option, id, prop, value) {

  ...

  if (option == "delete") {
    if (prop == "cars") {
        ex[id][prop] = ex[id][prop].filter((_, i) => !value.includes(i));       
    }
  }

  return ex;
};

updateFunction("delete","list_1","cars", [0, 2, 3])

Adapted to delete by either index or value.

function updateFunction(option, id, prop, value) {

  ...

  if (option == 'delete') {
    if (prop == 'cars') {
      ex[id][prop] = ex[id][prop].filter((v, i) => !value.filter.includes(value.byValue ? v : i));
    }
  }

  return ex;
};

updateFunction('delete', 'list_1', 'cars', { filter: [0, 2, 3], byValue: false });
updateFunction('delete', 'list_2', 'cars', { filter: ['fiat'], byValue: true });

  • Related