使用多维数据展平javascript数组

时间:2021-03-31 21:30:43

How can one flatten this in javascript?

如何在javascript中压扁这个?

[
    [ task: 2 ],
    [ status: 'REFUND' ],
    [],
    [ amount: 872.2 ] 
]

To something like:

对于这样的事情:

[task: 2, status: 'REFUND', amount: 872.2]

Tried several options including Array.prototype.reduce() but did not work for me.

尝试了几个选项,包括Array.prototype.reduce(),但不适合我。

4 个解决方案

#1


1  

You can use join()

你可以使用join()

A good starting point:

一个很好的起点:

array[0] = array[0].join(", ");

#2


1  

you can use underscore.js

你可以使用underscore.js

var u = require('underscore');
u.reduce(a, function(memo, item) { 
    var o = {}; 
    u.each(memo, function(v, k) { 
        o[k] = v; 
    }); 
    u.each(item, function(v, k) { 
        o[k] = v; 
    }); 
    return o;
});


Input:

[
    { task: 2 },
    { status: 'REFUND' },
    {},
    { amount: 872.2 }
]

Output:

{ 
   task: 2,
   status: 'REFUND',
   amount: 872.2 
}

#3


0  

Your syntax is invalid; there is no : allowed outside of an object literal.

你的语法无效;没有:允许在对象字面之外。

var arr = [
  [{task: 2}],[{status: 'REFUND'}],[],[{amount: 872.2}]
];

To flatten your array, use the versatile reduce:

要展平阵列,请使用多功能减少:

var result = arr.reduce(function(a,b) {
  return a.concat(b)
}); //result.length === 3

Your result contains 3 elements as [] is filter out

您的结果包含3个元素,因为[]被过滤掉了

reduce is part of ECMAScript 5th edition. Ensure your browser supports it.

reduce是ECMAScript第5版的一部分。确保您的浏览器支持它。

#4


0  

Please check this one.

请检查一下。

var a = [], b = [],c = [],d=[]; 
 a['task'] = 2;
 b['status'] = 'REFUND';
 d['amount']= 872.2

 c.push(a); 
 c.push(b);
 c.push(d) 
 var arr = [];
console.log(c)

for(var key in c){
    //console.log(Object.keys(c[key]),c[key][Object.keys(c[key])]);
     arr[Object.keys(c[key])]=c[key][Object.keys(c[key])];
   }

 console.log(arr)  // please see the console.

Here is the plunker

这是plunker

#1


1  

You can use join()

你可以使用join()

A good starting point:

一个很好的起点:

array[0] = array[0].join(", ");

#2


1  

you can use underscore.js

你可以使用underscore.js

var u = require('underscore');
u.reduce(a, function(memo, item) { 
    var o = {}; 
    u.each(memo, function(v, k) { 
        o[k] = v; 
    }); 
    u.each(item, function(v, k) { 
        o[k] = v; 
    }); 
    return o;
});


Input:

[
    { task: 2 },
    { status: 'REFUND' },
    {},
    { amount: 872.2 }
]

Output:

{ 
   task: 2,
   status: 'REFUND',
   amount: 872.2 
}

#3


0  

Your syntax is invalid; there is no : allowed outside of an object literal.

你的语法无效;没有:允许在对象字面之外。

var arr = [
  [{task: 2}],[{status: 'REFUND'}],[],[{amount: 872.2}]
];

To flatten your array, use the versatile reduce:

要展平阵列,请使用多功能减少:

var result = arr.reduce(function(a,b) {
  return a.concat(b)
}); //result.length === 3

Your result contains 3 elements as [] is filter out

您的结果包含3个元素,因为[]被过滤掉了

reduce is part of ECMAScript 5th edition. Ensure your browser supports it.

reduce是ECMAScript第5版的一部分。确保您的浏览器支持它。

#4


0  

Please check this one.

请检查一下。

var a = [], b = [],c = [],d=[]; 
 a['task'] = 2;
 b['status'] = 'REFUND';
 d['amount']= 872.2

 c.push(a); 
 c.push(b);
 c.push(d) 
 var arr = [];
console.log(c)

for(var key in c){
    //console.log(Object.keys(c[key]),c[key][Object.keys(c[key])]);
     arr[Object.keys(c[key])]=c[key][Object.keys(c[key])];
   }

 console.log(arr)  // please see the console.

Here is the plunker

这是plunker