0

I'm doing the push method between two Arrays to create a bigger Array. The two simple arrays I want to fix are:

[{"id":"11"},{"color":"blue","value":"14"}] 
[{"id":"11"},{"color":"green","value":"25"}] 

The code to push the two arrays is:

var totjunt = $('body').data('cesta_list').push(array_of_bought_colors_new);

With $('body').data('cesta_list'); I save the first array and then i try to push the second array.

Using console.log(JSON.stringify(totjunt)); I print the value throught the console but the problem is that the console prints only a number 2.

1

2 Answers 2

4

You're logging the result of the push() call, not the resulting array. Try this:

$('body').data('cesta_list').push(array_of_bought_colors_new);
var totjunt = $('body').data('cesta_list');

More specifically, push() returns the length of the new array, not the array itself.

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

Comments

3

.push doesn't return a new array. It returns the array's new length. The array is updated in-place.

1 Comment

@jmar777: Knock yourself out, enjoy the link :)

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.