0

I have the array:

[ { x: 0,
    y: 1,
    z: 2,
    w: 3 },
  { x: 1,
    y: 2',
    z: 3 } ]

How can I pop "w"? I need the result:

[ { x: 0,
    y: 1,
    z: 2 },
  { x: 1,
    y: 2',
    z: 3 } ]
0

1 Answer 1

3

w isn't in an array, it's in an object (which is in an array, as the first entry). You can read its value like this:

var w = theArray[0].w;

you can remove it from the object like this:

delete theArray[0].w;

There's no built-in single operation that reads the value and deletes it, like Array#pop or Array#splice do.


Note: It usually doesn't matter, but deleting properties from objects on modern JavaScript engines usually has a negative impact on the performance of reading properties from the object afterward. This is because modern engines create on-the-fly classes for objects (and then subclass them when you add more properties), but deleting a property causes the object to fall back to the much-less-optimized "dictionary" (e.g., map) mode. Of course, this varies by JavaScript engine.

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

Comments

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.