Reputation: 829
I have an array:
var Array = {};
Array['elements'] = [];
Array['elements'][0]['name'] = "Sally";
Array['elements'][0]['age'] = "20";
Array['elements'][3]['name'] = "Jack";
Array['elements'][3]['age'] = "21";
Array['elements'][4]['name'] = "Megan";
Array['elements'][4]['age'] = "22";
I do not know how to remove Jack from the array to return a list of women only. Splice is mutable, and passing back the entire object seems inefficient, and cumbersome.
In PHP, you can:
unset($Array['elements'][3]);
I have tried
delete Array['elements'][3];
but that just yields a null value in its place, not really deleting the element, messing up everywhere else I am testing for elements. Is there a clean way to delete an element from an array based on its key?
Thanks!
Upvotes: 0
Views: 96
Reputation: 41
if you want to remove Jack from the array, you can code like this:
Array['elements'].filter(e => e.name != 'Jack');
Upvotes: 1
Reputation: 6264
Firstly, don't use Array
as an variable name - it is the javascript Array object, you don't want to override that
Secondly, since Array = {}
, calling it any variation of the word Array
is misleading, it's an Object
Thirdly, you've created Array.elements as an array, but then populate only indices 0, 3, and 4 - so you've created a sparse array to begin with
Fourthly, the code you wrote doesn't even run, since you are trying to assign .name
to elements[0]
but at that point elements[0]
is undefined, so you'll end up with an error, and the code stops running right there
What it looks like you want is Array.elements = {}
- then you can add/delete any key you want
For example
var obj = {};
obj.elements = {};
obj.elements[0] = {};
obj.elements[0].name = "Sally";
obj.elements[0].age = "20";
obj.elements[3] = {};
obj.elements[3].name = "Jack";
obj.elements[3].age = "21";
obj.elements[4] = {};
obj.elements[4].name = "Megan";
obj.elements[4].age = "22";
delete obj.elements[3];
for (let i in obj.elements) {
console.log('Number', i, 'is', obj.elements[i].name);
}
When using elements as an Array, you'll see after your initial population of the array, you already have two undefined to begin with - see first console output
Note: however, for...in
loop will skip those undefined elements
Also note, the undefined elements are actually "empty slots" - i.e. if you simply put obj.elements[3] = undefined
that's not the same as delete obj.elements[3]
var obj = {};
obj.elements = [];
obj.elements[0] = {};
obj.elements[0].name = "Sally";
obj.elements[0].age = "20";
obj.elements[3] = {};
obj.elements[3].name = "Jack";
obj.elements[3].age = "21";
obj.elements[4] = {};
obj.elements[4].name = "Megan";
obj.elements[4].age = "22";
console.log(obj.elements)
delete obj.elements[3];
console.log(obj.elements)
for (let i in obj.elements) {
console.log('Number', i, 'is', obj.elements[i].name);
}
Upvotes: 1
Reputation: 14302
You can remove items from an array by splicing it:
var a = [1,2,3,4,5]
var removed = a.splice(1,2)
a
becomes:
[1,4,5]
And removed
contains:
[2,3]
You can select a new, filtered list by referring to Nidhin's answer.
As an aside, the code in your question is not JavaScript... Are you running that gibberish successfully somewhere?
Upvotes: 0
Reputation: 124
Try this, it uses splice though but it will leave your initial array as it is -
let array = [];
array.push({name:'Sally', age:20});
array.push({name:'Jack', age:21});
array.push({name:'Megan', age:22});
let newArray = [...array];
newArray.splice(1, 1);
Check this article https://jaketrent.com/post/remove-array-element-without-mutating/
Upvotes: 1