0

i want to push value multidimensional array. But I could not success.

var e = [];
var data = []; 
var element = {}, items = [];
e = getelement("alan"); 
for(s=0;s < e.length ; s++ ){
element.resim = $( "#"+e[s] ).val();
element.baslik = $( "#"+e[s] ).val();
element.icerik = $( "#"+e[s] ).val();
element.links = $( "#"+e[s] ).val();
items.push(element);
}
c = JSON.stringify(items);

e object source:

'0' => "resim" '1' => "baslik" '2' => "icerik" '3' => "link"

c object source:

[
 {"resim":"4","baslik":"4","icerik":"4","links":"4"},
 {"resim":"4","baslik":"4","icerik":"4","links":"4"},
 {"resim":"4","baslik":"4","icerik":"4","links":"4"},
 {"resim":"4","baslik":"4","icerik":"4","links":"4"}
]
AlliterativeAlice
12.7k9 gold badges56 silver badges73 bronze badges
asked Jun 7, 2015 at 5:14
1
  • 2
    What is the expected result? Can you please describe what you're trying to achieve, so we here can help? Commented Jun 7, 2015 at 5:22

2 Answers 2

2

You only ever store a single object in element

Each time you go around the loop you edit the existing object and then push another reference to it onto the array.

Create a new object each time you go around the loop.

for(s=0;s < e.length ; s++ ){
 element = {};
answered Jun 7, 2015 at 5:26
Sign up to request clarification or add additional context in comments.

Comments

1

You aren't creating a new object every time you push to the array, so you're just modifying the same object and pushing it into the array 4 times. You need to create a new object each time you loop through like so:

var e = [];
var data = []; 
var items = [];
e = getelement("alan"); 
for(s=0;s < e.length ; s++ ){
 var element = {};
 element.resim = $( "#"+e[s] ).val();
 element.baslik = $( "#"+e[s] ).val();
 element.icerik = $( "#"+e[s] ).val();
 element.links = $( "#"+e[s] ).val();
 items.push(element);
}
c = JSON.stringify(items);
answered Jun 7, 2015 at 5:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.