16

I have an array like this:

var oldArray = [{'value': '1', 'label': 'a'}, {'value': '2', 'label': 'b'}]

what I want is using spread operator add a new object at the beginning of that array:

BTW this works:

var oldArray = [{'value': '1', 'label': 'a'}, {'value': '2', 'label': 'b'}]
var newObj = {'value': 'all', 'label': 'all'}
var result = [newObj, ...oldArray]

But generates a key "newObj" like this:

var oldArray = [newObj : {'value': 'all', 'label': 'all'}, 0: {'value': '1', 'label': 'a'}, 1:{'value': '2', 'label': 'b'}]

And I want the key to be auto generated like if I do this:

var result = [{'value': 'all', 'label': 'all'}, ...oldArray]

And imagine the result is this:

var oldArray = [newObj : {0: 'all', 'label': 'all'}, 1: {'value': '1', 'label': 'a'}, 2:{'value': '2', 'label': 'b'}]

but that gives me an error.

Right now I'm using unshift and it works, I wonder if there's a way to do the same with spread operator.

5
  • 3
    Can you not just use unshift()? Commented Apr 28, 2017 at 14:38
  • your answer here: stackoverflow.com/questions/8073673/… Commented Apr 28, 2017 at 14:42
  • 4
    var result = [newObj, ...oldArray] also gives the correct output. am i missing something ? Commented Apr 28, 2017 at 14:43
  • 1
    So many answers suggesting unshift but that is not what you asked. var result = [newObj, ...oldArray] does work and is the right answer, the problem is you have a false premise in your question... Commented Apr 28, 2017 at 14:54
  • 1
    @AluanHaddad I've said that var result = [newObj, ...oldArray] works and unshift works is what I've used. But I'm using a drop down library that takes the resulting array to generate the drop down, and result = [newObj, ...oldArray] generates a key value that brokes the library. I've corrected the question removing the false premise. Commented Apr 30, 2017 at 3:58

3 Answers 3

11

You can solve this problem modifying it like this with spread:

change:

var result = [newObj, ...oldArray]

to:

var result = [{...newObj}, ...oldArray]
Sign up to request clarification or add additional context in comments.

1 Comment

it will show [{}, oldArray] if newObj is empty.
4

You could use the unshift() function which does exactly that:

let oldArray = [{'value': '1', 'label': 'a'}, {'value': '2', 'label': 'b'}]
let newObj = {'value': 'all', 'label': 'all'}
oldArray.unshift(newObj)

console.log(oldArray)

Or if you don't want to modify the original array, you can do:

const result = oldArray.slice()
result.unshift(newObj)
console.log(result)

4 Comments

What is wrong with the unshift()? It seems to work as shown in George's code snippet in his answer below. Why the -1? Is there a drawback to using unshift? (Edit:) oh, because it's not using the spread operator as mentioned in the question?
@downvoter Could've mentioned that w/o the downvote... SO becomes a more hostile site day by day.
You did not answer the question asked.
@AP. Yes, SO/SE are known to be very hostile, everywhere on the Internet people talk about the negativity here. I agree. Downvoting, downvoting, downvoting, sometimes for nothing.
1

You can use unshift() to add items to the beginning of an array

var oldArray = [{'value': '1', 'label': 'a'}, {'value': '2', 'label': 'b'}]
var newObj = {'value': 'all', 'label': 'all'}
oldArray.unshift(newObj);

console.log(oldArray);

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.