10

I'm new in JS, can't find solution to do something like that

var arr = [0];
var elem = [1, 2, 3];
???
console.log(arr); // shows [0, [1, 2, 3]];

I've tried with .push(elem), JS decides that I passed array of values (not a single one), and concatenate content of arr and elem arrays, so that the result is [0, 1, 2, 3]

7
  • this might do the trick w3schools.com/jsref/jsref_concat_array.asp Commented May 20, 2017 at 22:00
  • 2
    arr.push(elem) should do exactly what you need. Commented May 20, 2017 at 22:01
  • 2
    @vidi that is a bad source, refer to this instead: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 20, 2017 at 22:02
  • 1
    @dfsq Incorrect. Calling arr.push() is adding the 2nd array as an item of that array. it's not merging the 2 arrays. Commented May 20, 2017 at 22:02
  • 2
    We cannot seem to figure out what you actually are trying to do :). Do you want [0, 1, 2, 3] or [0, [1, 2, 3]]? Commented May 20, 2017 at 22:08

4 Answers 4

10

Use concat!

var arr = [0];
var elem = [1, 2, 3];
var newArr = arr.concat([elem]);
console.log(newArr); // => [0,[1,2,3]]
Sign up to request clarification or add additional context in comments.

3 Comments

Could you explain, how does it works?) Does wrapping with square brackets equivalent to new Array(elem)?
concat is an Array method. It can be called on an array. We are doing it arr.concat. It will parse the given elements. If the given prop is an array like: arr.concat(elem), it would parse all those elements and output: [0,1,2,3,4], but if we put square brackets around the elem array...
Now I think I've got this. It can also be done with arr.push([elem]), but in that case the source array will be modified. So, to summarize, the answer is "wrapping with square brackets"
3

Now, after you wrote, what you want,

[0, [1, 2, 3]]

you could use at lease three different approaches:

  1. Simple assignment at the end of the array

    var arr = [0],
        elem = [1, 2, 3];
    
    arr[arr.length] = elem;
    console.log(arr);

  2. Array#push for pushing a value/object at the end of an array, which is basically the same as above without indicating the place for inserting, but you can use more item for pusing to the array.

    var arr = [0],
        elem = [1, 2, 3];
    
    arr.push(elem);
    console.log(arr);

  3. Array#concat, creating a new array with with the given array and the parameters. Here cou need to wrap the content in an array, because concat concatinates arrays.

    var arr = [0],
        elem = [1, 2, 3];
    
    arr = arr.concat([elem]);
    console.log(arr);

1 Comment

You were almost right. @Markus Ivancsics proposed arr.concat([elem]) that is worked
3

You may try to use spread operator to concatenate values of an array.
For example:

let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];

console.log(arr2);
//Output: [ 1, 2, 3, 4, 5 ]

1 Comment

True, and not what the question is asking for. (though the Q might have been modified between original ask, and now)
1

you can use this method to push an array into another array

var arr = [0];
var elem = [1, 2, 3];

arr.push(...elem)

console.log(arr); // shows [0, 1, 2, 3];

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.