0

I have an array and I want to put it in another array using indexes.

For example:

arry[1].push(sub_array_1)
array[2].push (sub_array_2)

But I get an error if I write:

var sub_array_1 = [1, 2, 2, 2, 2];

arry[1].push(sub_array_1)
4
  • 1
    Are you going to tell us what the error is or do we have to guess? I don't like the guessing game. In addition, what's the value of arry and array? Commented Sep 26, 2014 at 21:54
  • what is array variable? Commented Sep 26, 2014 at 21:54
  • Start off by reading the documentation for Array#push and array element access. Commented Sep 27, 2014 at 2:48
  • This question appears to be off-topic because it is about rudimentary behavior of push which is easily answered by RTFM. Commented Sep 27, 2014 at 2:49

5 Answers 5

5

Using spread operator

var subArray = [1, 4, 6, 7];
var mainArray = [6, 7, 8];
var index = 1;
mainArray = [...mainArray.slice(0, index), subArray, ...mainArray.slice(index)];
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming:

var arry = [9,8,7];
var sub_array_1 = [1,2,2,2,2];
  1. If you are trying to insert sub_array_1 into arry, as a single element, just use splice directly:

    arry.splice(1, 0, sub_array_1);
    

    The result will be:

    [9,[1,2,2,2,2],8,7]
    
  2. On the other hand, if you are trying to insert the contents of sub_array_1 before the second element of arry, you can do something like this:

    Array.prototype.splice.apply(arry, [1, 0].concat(sub_array_1));

    The result will be:

    [9,1,2,2,2,2,8,7]
    

    Here is a more general function:

    function insert(arrayDest, index, arraySrc) {
      Array.prototype.splice.apply(arrayDest, [index, 0].concat(arraySrc));
    }
    

    [EDITED]

    Starting with ES6, you can simplify the above code using the spread operator (...). For example:

    function insert(arrayDest, index, arraySrc) {
      arrayDest.splice(index, 0, ...arraySrc);
    }
    

3 Comments

Since arry is an array why not just us arry.splice()?
Because splice expects the inserted elements to be passed as individual values, not as an array.
Ah. I think your understanding of the question is wrong. He wants to insert the sub_array into the array. Not the contents of sub_array. If you feel that your interpretation is correct please ask the OP to clarify.
1

You're using wrong syntax! Follow the either below mentioned approach.

var sub_array_1 = [1,2,2,2,2];
arry[1] = sub_array_1;

// OR

var sub_array_1 = [1,2,2,2,2];
arry.push(sub_array_1);

.push(ele) will add an item to an array, thereby incrementing the length of array by 1. Remember array index starts at 0.

If you need to add an item(array/object/other) to a particular index, use [index]. Eg: arry[0] = [1,23]; arry[1] = [4,5,6,7];

1 Comment

Hope it solves your problem :) Please mark it answer by ticking :)
0

let array = []
array.push({"index": 0, "value":100})
console.log(array)

maybe it helping for you

Comments

0
obj.arrayOne.push(arrayLetters);

or

obj['arrayOne'].push(arrayLetters);

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.