0

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.

0

2 Answers 2

1

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;
}

Sign up to request clarification or add additional context in comments.

4 Comments

And if I had 4 elements in the cars array and I want to delete the second element or the third ?
then you can use ex[id][prop].splice(0, 3)
splice - delete first , second and third element ?
Yup, try it It delete 3 elements from the 0 position
0

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])

var ex = {
  "list_1": {
    "money": 1000,
    "manager": "Louis",
    "cars": ["mazda", "ford_focus", "fiat", "dacia"]
  },
  "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);
    }
  }
  
  if (option == "delete") {
    if (prop == "cars") {
        ex[id][prop] = ex[id][prop].filter((_, i) => !value.includes(i));       
    }
  }

  return ex;
};

updateFunction("update", "list_1", "manager", "Andrew");


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

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

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 });

var ex = {
  list_1: {
    money: 1000,
    manager: 'Louis',
    cars: ['mazda', 'ford_focus', 'fiat', 'dacia'],
  },
  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);
    }
  }

  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('update', 'list_1', 'manager', 'Andrew');

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.