When push a data into array is pushing in just one index
Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);
but the result I getting is some like this
I put all my new array inside a index of my old array what am I doing wrong ? can some one help me
javascript jquery arrays
add a comment |
Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);
but the result I getting is some like this
I put all my new array inside a index of my old array what am I doing wrong ? can some one help me
javascript jquery arrays
This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.
– Bucket
Nov 15 '18 at 16:16
possible duplicate of stackoverflow.com/questions/1374126/…
– Tanmay
Nov 15 '18 at 16:33
add a comment |
Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);
but the result I getting is some like this
I put all my new array inside a index of my old array what am I doing wrong ? can some one help me
javascript jquery arrays
Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);
but the result I getting is some like this
I put all my new array inside a index of my old array what am I doing wrong ? can some one help me
javascript jquery arrays
javascript jquery arrays
asked Nov 15 '18 at 16:13
Alfredo IzquierdoAlfredo Izquierdo
287
287
This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.
– Bucket
Nov 15 '18 at 16:16
possible duplicate of stackoverflow.com/questions/1374126/…
– Tanmay
Nov 15 '18 at 16:33
add a comment |
This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.
– Bucket
Nov 15 '18 at 16:16
possible duplicate of stackoverflow.com/questions/1374126/…
– Tanmay
Nov 15 '18 at 16:33
This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.
– Bucket
Nov 15 '18 at 16:16
This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.
– Bucket
Nov 15 '18 at 16:16
possible duplicate of stackoverflow.com/questions/1374126/…
– Tanmay
Nov 15 '18 at 16:33
possible duplicate of stackoverflow.com/questions/1374126/…
– Tanmay
Nov 15 '18 at 16:33
add a comment |
2 Answers
2
active
oldest
votes
You have to use concat
but remember....
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
var combined = old.concat(response.DATA);
console.log(combined);
);
add a comment |
Use Array.ptototype.concat() instead to merge the arrays.Array.prototype.concat()
:
The concat() method is used to merge two or more arrays. This method
does not change the existing arrays, but instead returns a new array.
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old = old.concat(response.DATA);
console.log(old);
);
var old = [
id: 1 ,
id: 2
];
console.log('old: ', old);
var json = [
id: 3 ,
id: 4
];
old = old.concat(json);
console.log('merged: ', old);
Hopefully that helps!
I use concat before but the element return is the same array
– Alfredo Izquierdo
Nov 15 '18 at 16:19
I try save each element as a new element in the old array
– Alfredo Izquierdo
Nov 15 '18 at 16:20
You are right, I missed a key portion in my answer.concat()
returns a new array so it would need to be saved to a variable. Updated the answer.
– Alexander Staroselsky
Nov 15 '18 at 16:21
@AlfredoIzquierdo You can reassign the value ofold
if the goal is to specifically useold
again in the method or elsewhere. I've updated the answer to reflect that.
– Alexander Staroselsky
Nov 15 '18 at 16:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323587%2fwhen-push-a-data-into-array-is-pushing-in-just-one-index%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to use concat
but remember....
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
var combined = old.concat(response.DATA);
console.log(combined);
);
add a comment |
You have to use concat
but remember....
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
var combined = old.concat(response.DATA);
console.log(combined);
);
add a comment |
You have to use concat
but remember....
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
var combined = old.concat(response.DATA);
console.log(combined);
);
You have to use concat
but remember....
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
var combined = old.concat(response.DATA);
console.log(combined);
);
answered Nov 15 '18 at 16:20
JamiecJamiec
94.8k1193152
94.8k1193152
add a comment |
add a comment |
Use Array.ptototype.concat() instead to merge the arrays.Array.prototype.concat()
:
The concat() method is used to merge two or more arrays. This method
does not change the existing arrays, but instead returns a new array.
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old = old.concat(response.DATA);
console.log(old);
);
var old = [
id: 1 ,
id: 2
];
console.log('old: ', old);
var json = [
id: 3 ,
id: 4
];
old = old.concat(json);
console.log('merged: ', old);
Hopefully that helps!
I use concat before but the element return is the same array
– Alfredo Izquierdo
Nov 15 '18 at 16:19
I try save each element as a new element in the old array
– Alfredo Izquierdo
Nov 15 '18 at 16:20
You are right, I missed a key portion in my answer.concat()
returns a new array so it would need to be saved to a variable. Updated the answer.
– Alexander Staroselsky
Nov 15 '18 at 16:21
@AlfredoIzquierdo You can reassign the value ofold
if the goal is to specifically useold
again in the method or elsewhere. I've updated the answer to reflect that.
– Alexander Staroselsky
Nov 15 '18 at 16:40
add a comment |
Use Array.ptototype.concat() instead to merge the arrays.Array.prototype.concat()
:
The concat() method is used to merge two or more arrays. This method
does not change the existing arrays, but instead returns a new array.
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old = old.concat(response.DATA);
console.log(old);
);
var old = [
id: 1 ,
id: 2
];
console.log('old: ', old);
var json = [
id: 3 ,
id: 4
];
old = old.concat(json);
console.log('merged: ', old);
Hopefully that helps!
I use concat before but the element return is the same array
– Alfredo Izquierdo
Nov 15 '18 at 16:19
I try save each element as a new element in the old array
– Alfredo Izquierdo
Nov 15 '18 at 16:20
You are right, I missed a key portion in my answer.concat()
returns a new array so it would need to be saved to a variable. Updated the answer.
– Alexander Staroselsky
Nov 15 '18 at 16:21
@AlfredoIzquierdo You can reassign the value ofold
if the goal is to specifically useold
again in the method or elsewhere. I've updated the answer to reflect that.
– Alexander Staroselsky
Nov 15 '18 at 16:40
add a comment |
Use Array.ptototype.concat() instead to merge the arrays.Array.prototype.concat()
:
The concat() method is used to merge two or more arrays. This method
does not change the existing arrays, but instead returns a new array.
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old = old.concat(response.DATA);
console.log(old);
);
var old = [
id: 1 ,
id: 2
];
console.log('old: ', old);
var json = [
id: 3 ,
id: 4
];
old = old.concat(json);
console.log('merged: ', old);
Hopefully that helps!
Use Array.ptototype.concat() instead to merge the arrays.Array.prototype.concat()
:
The concat() method is used to merge two or more arrays. This method
does not change the existing arrays, but instead returns a new array.
$.getJSON(setUrl(pagination, page), function (response)
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old = old.concat(response.DATA);
console.log(old);
);
var old = [
id: 1 ,
id: 2
];
console.log('old: ', old);
var json = [
id: 3 ,
id: 4
];
old = old.concat(json);
console.log('merged: ', old);
Hopefully that helps!
var old = [
id: 1 ,
id: 2
];
console.log('old: ', old);
var json = [
id: 3 ,
id: 4
];
old = old.concat(json);
console.log('merged: ', old);
var old = [
id: 1 ,
id: 2
];
console.log('old: ', old);
var json = [
id: 3 ,
id: 4
];
old = old.concat(json);
console.log('merged: ', old);
edited Nov 15 '18 at 16:28
answered Nov 15 '18 at 16:16
Alexander StaroselskyAlexander Staroselsky
13.7k42342
13.7k42342
I use concat before but the element return is the same array
– Alfredo Izquierdo
Nov 15 '18 at 16:19
I try save each element as a new element in the old array
– Alfredo Izquierdo
Nov 15 '18 at 16:20
You are right, I missed a key portion in my answer.concat()
returns a new array so it would need to be saved to a variable. Updated the answer.
– Alexander Staroselsky
Nov 15 '18 at 16:21
@AlfredoIzquierdo You can reassign the value ofold
if the goal is to specifically useold
again in the method or elsewhere. I've updated the answer to reflect that.
– Alexander Staroselsky
Nov 15 '18 at 16:40
add a comment |
I use concat before but the element return is the same array
– Alfredo Izquierdo
Nov 15 '18 at 16:19
I try save each element as a new element in the old array
– Alfredo Izquierdo
Nov 15 '18 at 16:20
You are right, I missed a key portion in my answer.concat()
returns a new array so it would need to be saved to a variable. Updated the answer.
– Alexander Staroselsky
Nov 15 '18 at 16:21
@AlfredoIzquierdo You can reassign the value ofold
if the goal is to specifically useold
again in the method or elsewhere. I've updated the answer to reflect that.
– Alexander Staroselsky
Nov 15 '18 at 16:40
I use concat before but the element return is the same array
– Alfredo Izquierdo
Nov 15 '18 at 16:19
I use concat before but the element return is the same array
– Alfredo Izquierdo
Nov 15 '18 at 16:19
I try save each element as a new element in the old array
– Alfredo Izquierdo
Nov 15 '18 at 16:20
I try save each element as a new element in the old array
– Alfredo Izquierdo
Nov 15 '18 at 16:20
You are right, I missed a key portion in my answer.
concat()
returns a new array so it would need to be saved to a variable. Updated the answer.– Alexander Staroselsky
Nov 15 '18 at 16:21
You are right, I missed a key portion in my answer.
concat()
returns a new array so it would need to be saved to a variable. Updated the answer.– Alexander Staroselsky
Nov 15 '18 at 16:21
@AlfredoIzquierdo You can reassign the value of
old
if the goal is to specifically use old
again in the method or elsewhere. I've updated the answer to reflect that.– Alexander Staroselsky
Nov 15 '18 at 16:40
@AlfredoIzquierdo You can reassign the value of
old
if the goal is to specifically use old
again in the method or elsewhere. I've updated the answer to reflect that.– Alexander Staroselsky
Nov 15 '18 at 16:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323587%2fwhen-push-a-data-into-array-is-pushing-in-just-one-index%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.
– Bucket
Nov 15 '18 at 16:16
possible duplicate of stackoverflow.com/questions/1374126/…
– Tanmay
Nov 15 '18 at 16:33