0
 var members = [
     ['Fred G. Aandahl', '1951-1953', 'North Dakota'],
     ['Watkins Moorman Abbitt', '1948-1973', 'Virginia'],
 ];

I need to create like this dynamically, I am using the following code to do that:

var data = new array();
var members = [];
$.each(data, function (i, elem) {
    data.push(elem["p_age"], elem["p_name"], elem["p_date"]);
    members.push(data);
});
console.log(members);
}

I need to print this values, for that.

for(var x = 0; x < members.length; x++) {
    console.log(members[i][0]);
    console.log(members[i][1]);
    console.log(members[i][2]);
}

so when i try this i get following.

  [object][object][object][object][object][object]
4
  • var members = [ ['Fred G. Aandahl', '1951-1953', 'North Dakota'], ['Watkins Moorman Abbitt', '1948-1973', 'Virginia'], ]; need to print the array like this. so i can use it further. Commented Aug 6, 2013 at 3:56
  • Your example starts with an empty array. Can you paste the actual code that creates var data? Commented Aug 6, 2013 at 4:00
  • Are you sure a multi-dimensional array like this is actually the best structure? An array of objects seems better suited to your problem. Commented Aug 6, 2013 at 4:00
  • How do you expect us to help you? You're just adding elements to your array, but who knows what's in them as you didn't provide any of the code. You're also not adding arrays to your array. Commented Aug 6, 2013 at 4:00

6 Answers 6

1

I am not sure how is your code working! It has some error's if your already aware of.

Your code should work fine after you change to:-

var data = new Array();//Was an Error in your code
var members = [];
$.each(temp, function (i, elem) {
    data.push(elem["p_age"], elem["p_name"], elem["p_date"]);
    members.push(data);
});

console.log(members);
for (var x = 0; x < members.length; x++) {
    console.log(members[x][0]);//Was an Error in your code
    console.log(members[x][1]);
    console.log(members[x][2]);
}

Secondly, how does data.push(elem["p_age"], elem["p_name"], elem["p_date"]); works for you? It should give you undefined.

Just to get myself clear I wrote down your code to a fiddle. Have a look.

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

Comments

1

Try

var members=[];
$.each(data, function(i, elem) {
    members.push([elem["p_date"],elem["p_name"],elem["p_date"]]);
});
console.log(JSON.stringify(members))

Comments

1

This looks suspect:

$.each(data, function(i, elem) {
  data.push(elem["p_date"],elem["p_name"],elem["p_date"]);

It looks like you're trying to iterate over data, pushing the elements back on to data. I imagine that the $.each() needs to iterate over something else.

I also question why you're pushing elem['p_date'] onto an array twice.

Comments

1

Because it is treating them as objects. Try using toString() method.

Comments

1

Hi use x instead of i for loop.

for(var x=0;x<members.length;x++){
                      console.log(members[x][0]);
                      console.log(members[x][1]);
                       console.log(members[x][2]);
                 }

It will work.

Comments

1

Not

var data=new array();

but

var data=new Array();

Array's class name is Array, but not 'array'.

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.