0

Using JavaScript how can I remove an object from an array and return that object? For example, change this:

[{first:"John", last:"Smith"}]

...to this:

{first:"John", last:"Smith"}

http://plnkr.co/edit/VqBCsXGuMM54MwHIKW3c?p=preview

2
  • 2
    You will have to show a little more context of the variable construction for a complete answer. Is this array assigned to a variable by itself or part of some other object?
    – jfriend00
    Jul 30, 2015 at 18:06
  • @jfriend00 It's assigned to a variable by itself - Plunker added.
    – Ryan
    Jul 30, 2015 at 18:55

2 Answers 2

4

Use splice. It will remove some items from the array and return them.

var data = [{first:"John", last:"Smith"}];
var extract = data.splice(0, 1);
console.log(extract, data); // will print: {first..., last...}, []

Note that splice does return an array itself, so you'll have to take the appropriate elements from that.

If you only have a single element, you can splice, pop, or simply take the element by index and then truncate the array.

7
  • .splice is returning an array with an object (not just an object). Am I missing something? Compare to myObject: plnkr.co/edit/Pg3rLv2Lspj118i34EBa?p=preview
    – Ryan
    Jul 30, 2015 at 18:42
  • @Ryan I mentioned that in the answer. splice will remove the elements from the original array and return a new array, which you have to take the element from normally (data.splice(0, 1)[0] or the like). It does the important step of removing, especially since it can remove element(s) from the middle of another array efficiently.
    – ssube
    Jul 30, 2015 at 18:43
  • So that's my question, how can I return just an object (not an array with an object)? I'm getting undefined when I try .pop(): plnkr.co/edit/Pg3rLv2Lspj118i34EBa?p=preview
    – Ryan
    Jul 30, 2015 at 18:47
  • You can use data.splice(0, 1).pop() or data.splice(0, 1)[0] or any number of other methods. There is no built-in method (that I know of) to splice a single element out of an array and return it as an object, probably because that behavior wouldn't make sense for multiple elements.
    – ssube
    Jul 30, 2015 at 18:50
  • data.splice(0, 1).pop(); and data.splice(0, 1)[0] both return undefined plnkr.co/edit/Pg3rLv2Lspj118i34EBa?p=preview
    – Ryan
    Jul 30, 2015 at 18:55
0

You could use the pop() method. It will always remove the last element from array and handle it to you;

Example:

var myArray = [{first:"John", last:"Smith"}]
var myObject = myArray.pop()

Now your myObject value will be {first:"John", last:"Smith"} and your myArray will be empty

If you had more than one items:

var myArray = [{first:"John", last:"Smith"}, {first:"Test", last:"Now"}]
var myObject = myArray.pop()

What happens here is that your myArray will have just the first value and the myObject will have the last one {first:"Test", last:"Now"}

You could also take a look at splice()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.