0

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!

2
  • 1
    Your code as written doesn't work since Array.elements[0] [3] and [4] are never defined, you can't add properties to them - firstly, I'd never overload Array, I wouldn't call that variable any variation of Array since it's not an Array; and finally just test for null if you insist on .elements being an Array (you could make it an Object instead, and you won't end up with a sparse array) Commented Oct 12, 2019 at 2:38
  • why are you adding to indices 0, 3 and 4 only? What is the significance of these numbers? Commented Oct 12, 2019 at 2:43

4 Answers 4

1

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/

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

Comments

1

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

Comments

1

if you want to remove Jack from the array, you can code like this:

Array['elements'].filter(e => e.name != 'Jack');

Comments

0

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?

5 Comments

then the indices for the entries will change
@Bravo Yes ... is that not the intention?
@svidgen Are you running that gibberish successfully somewhere? haha :D :D
@svidgen - since the initial attempt creates a sparse array, not sure what the intention is - it seems the indices are important (should use an object instead0
@Bravo Yeah, I'd read it as the indexes being incidental. But, you may be right. Post an answer!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.