JavaScript - JSON对象,值的分组和乘法

时间:2018-05-13 11:27:58

标签: javascript json

我有一个名为result的JSON输出:

{
  "status": "success",
  "data": [
    {"id": "39036", "name": "Steve", "product": "10", "count": "5", "cost": "4"},
    {"id": "39037", "name": "Steve", "product": "11", "count": "5", "cost": "7"},
    {"id": "39038", "name": "Steve", "product": "12", "count": "5", "cost": "2"},
    {"id": "39039", "name": "Steve", "product": "12", "count": "4", "cost": "2"},
    {"id": "39040", "name": "Steve", "product": "12", "count": "1", "cost": "2"}
  ]
}

我需要每个产品编号的产品编号,数量,成本和总数

即。对于上面,输出将是:

((product 10, count 5, cost 4, total 20),
(product 11, count 5, cost 7, total 35),
(product 12, count 10, cost 2, total 20))

我不知道从哪里开始,有没有人有任何指导?

3 个答案:

答案 0 :(得分:3)

您可以使用reduce方法创建一个带有值的对象,然后使用Object.values来获取对象数组。



const json = {"status" : "success", "data" : [{"id":"39036","name":"Steve","product":"10","count":"5","cost":"4"},{"id":"39037","name":"Steve","product":"11","count":"5","cost":"7"},{"id":"39038","name":"Steve","product":"12","count":"5","cost":"2"},{"id":"39039","name":"Steve","product":"12","count":"4","cost":"2"},{"id":"39040","name":"Steve","product":"12","count":"1","cost":"2"}] }

// Take { product, count, cost} from current element in reduce callback
const result = Object.values(json.data.reduce((r, { product, count, cost}) => {
  // Check if product exists in accumulator
  // If not set its value to a new object
  if (!r[product]) r[product] = { product, count, cost, total: count * cost}
  // If it does exists update count and total values
  // +count will convert string to number
  else {
    r[product].count = +r[product].count + +count;
    r[product].total = +r[product].count * +r[product].cost
  }
  return r;
}, {}))

console.log(result)




您也可以使用ES6 Map来存储数据,然后使用带有扩展语法的values()方法来获取数组。



const json = {"status" : "success", "data" : [{"id":"39036","name":"Steve","product":"10","count":"5","cost":"4"},{"id":"39037","name":"Steve","product":"11","count":"5","cost":"7"},{"id":"39038","name":"Steve","product":"12","count":"5","cost":"2"},{"id":"39039","name":"Steve","product":"12","count":"4","cost":"2"},{"id":"39040","name":"Steve","product":"12","count":"1","cost":"2"}] }

const map = json.data.reduce((r, {product,count,cost}) => {
  if (!r.has(product)) {
    r.set(product, { product, count, cost, total: count * cost})
  } else {
    const prod = r.get(product);
    prod.count = +prod.count + +count;
    prod.total = +prod.cost * +prod.count
  }
  return r;
}, new Map);

const result = [...map.values()]

console.log(result)




答案 1 :(得分:2)

正如你所提到的,你已经尝试用循环来做,可能值得展示它是如何工作的。



var json=
'{\
  "status": "success",\
  "data": [\
    {"id": "39036", "name": "Steve", "product": "10", "count": "5", "cost": "4"},\
    {"id": "39037", "name": "Steve", "product": "11", "count": "5", "cost": "7"},\
    {"id": "39038", "name": "Steve", "product": "12", "count": "5", "cost": "2"},\
    {"id": "39039", "name": "Steve", "product": "12", "count": "4", "cost": "2"},\
    {"id": "39040", "name": "Steve", "product": "12", "count": "1", "cost": "2"}\
  ]\
}';
var data=JSON.parse(json).data;              // only the array

var result={};
for(var i=0;i<data.length;i++){
  var order=data[i];
  if(result[order.product]){                 // such product is in the result already
    result[order.product].count+=parseInt(order.count);
  }else{                                     // product is something new
    result[order.product]={
      product:order.product,
      count:parseInt(order.count),
      cost:parseInt(order.cost),
    }
  }
}

// put result in a list
var resultlist=[];
for(var product in result){
  result[product].total=result[product].cost*result[product].count;
  resultlist.push(result[product]);
}
console.log(resultlist);
&#13;
&#13;
&#13;

然后以reduce()作为捷径。

答案 2 :(得分:-1)

任务描述

  • JSON可以表示为字符串
  • 然后需要将JSON字符串解析为对象JSON.parse
  • 制作输出数组
  • 遍历项目
  • 对于每个项目,获取所需的数据,创建关联数组{},并将其推送到输出的末尾

代码

var json = '{"status" : "success", "data" : [{"id":"39036","name":"Steve","product":"10","count":"5","cost":"4"},{"id":"39037","name":"Steve","product":"11","count":"5","cost":"7"},{"id":"39038","name":"Steve","product":"12","count":"5","cost":"2"},{"id":"39039","name":"Steve","product":"12","count":"4","cost":"2"},{"id":"39040","name":"Steve","product":"12","count":"1","cost":"2"}] }'

var res = JSON.parse(json);

var data = res.data;

var output = [];

for (var i = 0; i < data.length; i++) {
  // now you can access the data for each individual item
  // and add it to the output with the total
  output.push({
    product: data[i].product,
    count: parseInt(data[i].count), 
    cost: parseInt(data[i].cost),
    total: parseInt(data[i].cost) * parseInt(data[i].count)});
}