最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pushing value into a multidimensional array - Stack Overflow

programmeradmin0浏览0评论

I have surfed the problem but couldn't get any possible solution ..

Let's say i have a var like this

var data = [
           {
              'a':10,
              'b':20,
              'c':30
           },
           {
              'a':1,
              'b':2,
              'c':3
           },
           {
              'a':100,
              'b':200,
              'c':300
           }];

Now , i need a multidimensional array like

var values = [[10,1,100],    //a
              [20,2,200],    //b
              [30,3,300]];   //c

What i have tried is

var values = [];
for(var key in data[0])
{
   values.push([]);   // this creates a multidimesional array for each key
   for(var i=0;i<data.length;i++)
   {
     // how to push data[i][key] in the multi dimensional array
   }
}

Note : data.length and number of keys keeps changing and i just want to be done using push() without any extra variables. Even i don't want to use extra for loops

If you guys found any duplicate here , just put the link as ment without downvote

I have surfed the problem but couldn't get any possible solution ..

Let's say i have a var like this

var data = [
           {
              'a':10,
              'b':20,
              'c':30
           },
           {
              'a':1,
              'b':2,
              'c':3
           },
           {
              'a':100,
              'b':200,
              'c':300
           }];

Now , i need a multidimensional array like

var values = [[10,1,100],    //a
              [20,2,200],    //b
              [30,3,300]];   //c

What i have tried is

var values = [];
for(var key in data[0])
{
   values.push([]);   // this creates a multidimesional array for each key
   for(var i=0;i<data.length;i++)
   {
     // how to push data[i][key] in the multi dimensional array
   }
}

Note : data.length and number of keys keeps changing and i just want to be done using push() without any extra variables. Even i don't want to use extra for loops

If you guys found any duplicate here , just put the link as ment without downvote

Share Improve this question edited May 28, 2013 at 11:40 Prasath K asked May 22, 2013 at 10:43 Prasath KPrasath K 5,0187 gold badges26 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Try this:

var result = new Array();

for(var i = 0; i < data.length; i++) {
  var arr = new Array();
  for(var key in data[i]) {
    arr.push(data[i][key]);
  }
  result.push(arr);
}

also if you don't want the 'arr' variable just write directly to the result, but in my opinion code above is much more understandable:

for(var i = 0; i < data.length; i++) {
  result.push(new Array());
  for(var key in data[i]) {
    result[i].push(data[i][key]);
  }
}

Ok, based on your ment I have modified the the loop. Please check the solution and mark question as answered if it is what you need. Personally I don't understand why you prefer messy and hard to understand code instead of using additional variables, but that's totally different topic.

for(var i = 0; i < data.length; i++) {
  for(var j = 0; j < Object.keys(data[0]).length; j++) {
    result[j] = result[j] || new Array();
    console.log('result[' + j + '][' + i + ']' + ' = ' + data[i][Object.keys(data[i])[j]])
    result[j][i] = data[i][Object.keys(data[i])[j]];
  }
}
发布评论

评论列表(0)

  1. 暂无评论