0

I have this object

inputErrors:
value: Array(8)
0: "name"
1: "industry"
2: "address"
3: "crn"
4: "website"
5: "employeesNbr"
6: "phoneNumber"
7: "userRole"

if I get sent a param to a function called key which is basically either from name to userole, what statement can i use to the delete the whole value that contains that key? I used inputErrors.value.splice(key);

This reseted the whole object. What i need is ill be sent a key of kind string which will either be {name,industry...}, and what i will want is delete its corresponding index. so for example if they key sent is address and delete address it becomes:

0: "name"
1: "industry"
3: "crn"
4: "website"
5: "employeesNbr"
6: "phoneNumber"
7: "userRole"


Thank you
SImon Haddad
  • 412
  • 3
  • 14

2 Answers2

1

if you are okay with a new Array then you can use Array.filter().

If you want to modify the same array, then use findIndex to find the index of the key in the array then use

someArray.splice(x, 1); // to remove the element at index 'x'
Ajay Reddy
  • 1,327
  • 14
  • 19
1

If you need to preserve indexes to generate a sparse array, you can work directly with the object instead of using array methods (yes, arrays are objects in JavaScript).

delete inputErrors[inputErrors.indexOf('address')]
Aprillion
  • 16,686
  • 4
  • 48
  • 86