Javascript 将阵列对象移动到阵列中
我有一组类似的对象Javascript 将阵列对象移动到阵列中,javascript,arrays,Javascript,Arrays,我有一组类似的对象 [{ name : Client 1, total: 900, value: 12000 }, { name : Client 2, total: 10, value: 800 }, { name : Client 3, total: 5, value : 0 }] 我想从中得到3个数组,一个名称数组 [客户端1、
[{
name : Client 1,
total: 900,
value: 12000
}, {
name : Client 2,
total: 10,
value: 800
}, {
name : Client 3,
total: 5,
value : 0
}]
我想从中得到3个数组,一个名称数组
[客户端1、客户端2、客户端3]
和总数的数组
[900,10,5]
和一组值
[12000、800、0]
我原以为我能找到像地图或类似的东西,但我很困惑如何使用它。有人能帮我吗?使用函数
var arr=[{name:“Client 1”,总计:900,value:12000},{name:“Client 2”,总计:10,value:800},{name:“Client 3”,总计:5,value:0}];
var总计=arr.map(e=>e.total);
var name=arr.map(e=>e.name);
var值=arr.map(e=>e.value);
文件。写(“+totals+”);
var names=[];
var totals=[];
var values=[];
for(var i=0; i<objectArray.length; i++){
names.push(objectArray[i].name);
totals.push(objectArray[i].total);
values.push(objectArray[i].value);
}
var a = [{
name : "Client 1",
total: 900,
value: 12000
}, {
name : "Client 2",
total: 10,
value: 800
}, {
name : "Client 3",
total: 5,
value : 0
}];
var res = a.reduce(function(a,b){
return {
name: a.name.concat(b.name),
total: a.total.concat(b.total),
value: a.value.concat(b.value)
}
},{
name: [],
total:[],
value:[]
})
console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
文件。写(“+names+”);
var names=[];
var totals=[];
var values=[];
for(var i=0; i<objectArray.length; i++){
names.push(objectArray[i].name);
totals.push(objectArray[i].total);
values.push(objectArray[i].value);
}
var a = [{
name : "Client 1",
total: 900,
value: 12000
}, {
name : "Client 2",
total: 10,
value: 800
}, {
name : "Client 3",
total: 5,
value : 0
}];
var res = a.reduce(function(a,b){
return {
name: a.name.concat(b.name),
total: a.total.concat(b.total),
value: a.value.concat(b.value)
}
},{
name: [],
total:[],
value:[]
})
console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
文档。写入(“+values+”)代码>您可以使用具有所需键的对象作为数组
var names=[];
var totals=[];
var values=[];
for(var i=0; i<objectArray.length; i++){
names.push(objectArray[i].name);
totals.push(objectArray[i].total);
values.push(objectArray[i].value);
}
var a = [{
name : "Client 1",
total: 900,
value: 12000
}, {
name : "Client 2",
total: 10,
value: 800
}, {
name : "Client 3",
total: 5,
value : 0
}];
var res = a.reduce(function(a,b){
return {
name: a.name.concat(b.name),
total: a.total.concat(b.total),
value: a.value.concat(b.value)
}
},{
name: [],
total:[],
value:[]
})
console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
var data=[{name:'Client 1',total:900,value:12000},{name:'Client 2',total:10,value:800},{name:'Client 3',total:5,value:0}],
结果=函数(数组){
var r={};
array.forEach(函数(a){
Object.keys(a).forEach(函数(k){
r[k]=r[k]| |[];
r[k].推(a[k]);
});
});
返回r;
}(数据);
document.write('name:'+JSON.stringify(result.name,0,4)+'');
var names=[];
var totals=[];
var values=[];
for(var i=0; i<objectArray.length; i++){
names.push(objectArray[i].name);
totals.push(objectArray[i].total);
values.push(objectArray[i].value);
}
var a = [{
name : "Client 1",
total: 900,
value: 12000
}, {
name : "Client 2",
total: 10,
value: 800
}, {
name : "Client 3",
total: 5,
value : 0
}];
var res = a.reduce(function(a,b){
return {
name: a.name.concat(b.name),
total: a.total.concat(b.total),
value: a.value.concat(b.value)
}
},{
name: [],
total:[],
value:[]
})
console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
document.write('total:'+JSON.stringify(result.total,0,4)+'';
var names=[];
var totals=[];
var values=[];
for(var i=0; i<objectArray.length; i++){
names.push(objectArray[i].name);
totals.push(objectArray[i].total);
values.push(objectArray[i].value);
}
var a = [{
name : "Client 1",
total: 900,
value: 12000
}, {
name : "Client 2",
total: 10,
value: 800
}, {
name : "Client 3",
total: 5,
value : 0
}];
var res = a.reduce(function(a,b){
return {
name: a.name.concat(b.name),
total: a.total.concat(b.total),
value: a.value.concat(b.value)
}
},{
name: [],
total:[],
value:[]
})
console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
document.write('value:'+JSON.stringify(result.value,0,4)+'');
var names=[];
var totals=[];
var values=[];
for(var i=0; i<objectArray.length; i++){
names.push(objectArray[i].name);
totals.push(objectArray[i].total);
values.push(objectArray[i].value);
}
var a = [{
name : "Client 1",
total: 900,
value: 12000
}, {
name : "Client 2",
total: 10,
value: 800
}, {
name : "Client 3",
total: 5,
value : 0
}];
var res = a.reduce(function(a,b){
return {
name: a.name.concat(b.name),
total: a.total.concat(b.total),
value: a.value.concat(b.value)
}
},{
name: [],
total:[],
value:[]
})
console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
write('the object:'+JSON.stringify(result,0,4)+'')代码>如果我没记错,你需要从1创建3个数组吗?你可以这样想:
var names=[];
var totals=[];
var values=[];
for(var i=0; i<objectArray.length; i++){
names.push(objectArray[i].name);
totals.push(objectArray[i].total);
values.push(objectArray[i].value);
}
var a = [{
name : "Client 1",
total: 900,
value: 12000
}, {
name : "Client 2",
total: 10,
value: 800
}, {
name : "Client 3",
total: 5,
value : 0
}];
var res = a.reduce(function(a,b){
return {
name: a.name.concat(b.name),
total: a.total.concat(b.total),
value: a.value.concat(b.value)
}
},{
name: [],
total:[],
value:[]
})
console.log(res) // Object {name: Array[3], total: Array[3], value: Array[3]}
var name=[];
var总计=[];
var值=[];
对于(var i=0;i如果您对包含每个数组的对象没有问题,则以下Array.prototype.reduce将起作用:
为什么Array#map
而不是Array.prototype.map
?我的意思是,为什么要使用
符号?@MatíasFidemraizer,这是原型的一篇短文,请阅读这里Array.prototype.map
OP,注意这是ES6,所以如果浏览器还不支持它,您可能需要一个transpiler。@isvforall是的,这更好!对我来说,这是最佳答案;P Reduce是从数组中产生不同结果的答案。。。