Javascript 使用ES6重新构造JSON对象

Javascript 使用ES6重新构造JSON对象,javascript,json,ecmascript-6,Javascript,Json,Ecmascript 6,我有这样的想法: 轮胎:[{ 名称:“fancyProduct1”, 数量:1 }, { 名称:“fancyProduct1”, 数量:1 }, { 名称:“fancyProduct1”, 数量:1 }, { 名称:“fancyProduct2”, 数量:1 }]; 我要做的是 tires: [{ name: "fancyProduct1", quantity: 3 }, { name: "fancyProduct2", quantity: 1 }] 有什么好办法吗?你可以这

我有这样的想法:

轮胎:[{
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct2”,
数量:1
}];
我要做的是

tires: [{
  name: "fancyProduct1",
  quantity: 3
}, {
  name: "fancyProduct2",
  quantity: 1
}]

有什么好办法吗?

你可以这样做

var newTries = tires.map(n => (
 // logic for new array where you can get attributes of item in tires to create a new array. 
console.log(n);  // this can show what properties are available in the current item write it to the console. 
)};

希望这有帮助

你可以这样做

var newTries = tires.map(n => (
 // logic for new array where you can get attributes of item in tires to create a new array. 
console.log(n);  // this can show what properties are available in the current item write it to the console. 
)};

希望这有帮助

您可以使用
reduce
将数组分组为一个对象。使用
Object.values
将对象转换为数组

let tires=[{“name”:“fancyProduct1”,“quantity”:1},{“name”:“fancyProduct1”,“quantity”:1},{“name”:“fancyProduct2”,“quantity”:1}];
让result=Object.values(tires.reduce((c,{name,quantity})=>{
c[name]=c[name]|{name,数量:0}
c[名称].数量+=数量;
返回c;
}, {}));

控制台日志(结果)
您可以使用
reduce
将数组分组为一个对象。使用
Object.values
将对象转换为数组

let tires=[{“name”:“fancyProduct1”,“quantity”:1},{“name”:“fancyProduct1”,“quantity”:1},{“name”:“fancyProduct2”,“quantity”:1}];
让result=Object.values(tires.reduce((c,{name,quantity})=>{
c[name]=c[name]|{name,数量:0}
c[名称].数量+=数量;
返回c;
}, {}));

控制台日志(结果)使用Reduce将完成以下任务:

var products = { tires: [ {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct2", quantity: 1}] };

var result = products.tires.reduce((acc,current) => {

    if (!acc[current.name]) {
       acc[current.name] = { name: current.name, quantity: 0};
    }

    acc[current.name].quantity++;

    return acc;
}, {});

var resultArray = Object.values(result);
console.log(resultArray);

使用Reduce将实现以下目标:

var products = { tires: [ {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct1", quantity: 1}, {name: "fancyProduct2", quantity: 1}] };

var result = products.tires.reduce((acc,current) => {

    if (!acc[current.name]) {
       acc[current.name] = { name: current.name, quantity: 0};
    }

    acc[current.name].quantity++;

    return acc;
}, {});

var resultArray = Object.values(result);
console.log(resultArray);
您可以使用一个简单的调用来循环
数组
项,同时检查
结果
数组中是否存在迭代的
,并相应地执行逻辑

这是您的代码应该如何编写的:

var result = [];

tires.forEach(function(el) {
  let found = result.find(o => o.name === el.name);
  if (found) {
    found["quantity"] += el["quantity"];
  } else {
    result.push(el);
  }
});
演示:

var轮胎=[{
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct2”,
数量:1
}];
var结果=[];
轮胎。forEach(功能(el){
let found=result.find(o=>o.name==el.name);
如果(找到){
找到[“数量”]+=el[“数量”];
}否则{
结果:推挤(el);
}
});
控制台日志(结果)
您可以使用一个简单的调用来循环
数组
项,同时检查
结果
数组中是否存在迭代的
,并相应地执行逻辑

这是您的代码应该如何编写的:

var result = [];

tires.forEach(function(el) {
  let found = result.find(o => o.name === el.name);
  if (found) {
    found["quantity"] += el["quantity"];
  } else {
    result.push(el);
  }
});
演示:

var轮胎=[{
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct1”,
数量:1
}, {
名称:“fancyProduct2”,
数量:1
}];
var结果=[];
轮胎。forEach(功能(el){
let found=result.find(o=>o.name==el.name);
如果(找到){
找到[“数量”]+=el[“数量”];
}否则{
结果:推挤(el);
}
});

控制台日志(结果)主要思想是按名称对产品进行分组,并将其数量相加。请帮我理解你的问题。您正在尝试聚合数据吗?如果是出于报告目的,有几种报告工具可以帮助您。我正在尝试将现有的对象数组重组为新的ones@BobanStanojevic你查过我的下面了吗?是的,谢谢你!其主要思想是按名称对产品进行分组,并将其数量相加。请帮我理解你的问题。您正在尝试聚合数据吗?如果是出于报告目的,有几种报告工具可以帮助您。我正在尝试将现有的对象数组重组为新的ones@BobanStanojevic你查过我的下面了吗?是的,谢谢你!请为新数组添加逻辑,从中可以获取轮胎中项目的属性以创建新数组。要真正回答这个问题,请添加新数组的逻辑,在这里您可以获取轮胎中项目的属性以创建新数组。要真正回答这个问题