基于对象数组中的不同单位的总和值

时间:2016-06-12 13:16:43

标签: javascript jquery arrays json

我陷入困境,我有JSON响应,如

[{
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 acs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 acs",
    "period": "all",
    "in": "Evening"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 cs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 cs",
    "period": "all",
    "in": "Evening"
}];

所以,我想获得dose密钥,其中quantity + unit但是使用jQuery的响应我希望获得结果 例如 - 3个Ac,3个cs。

4 个答案:

答案 0 :(得分:2)

您可以使用 Array#forEach 来迭代对象数组,并创建和对象来存储结果。

// An object to store the result
var result = {};

// Iterate over array of objects
arr.forEach(function(obj) {
    // Extract quantity & unit
    var quantity = parseInt(obj.dose.match(/\d+/)[0], 10),
        unit = obj.dose.replace(/\d+/, '').trim();

    // If the unit already exists in the result object
    //    Add the quantity to it
    // else
    //    Store the quantity
    result[unit] = result[unit] ? result[unit] + quantity : quantity;
});

var arr = [{
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 acs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 acs",
    "period": "all",
    "in": "Evening"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 cs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 cs",
    "period": "all",
    "in": "Evening"
}];

var result = {};
arr.forEach(function(obj) {
    var quantity = parseInt(obj.dose.match(/\d+/)[0], 10),
        unit = obj.dose.replace(/\d+/, '').trim();
    result[unit] = result[unit] ? result[unit] + quantity : quantity;
});

console.log(result);

上面的代码将以对象格式给出结果,如果你想要它以数组格式添加

Object.keys(result).map(key => result[key] + ' ' + key)

位于代码的底部。

var arr = [{
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 acs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 acs",
    "period": "all",
    "in": "Evening"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 cs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 cs",
    "period": "all",
    "in": "Evening"
}];

var result = {};
arr.forEach(function(obj) {
    var quantity = parseInt(obj.dose.match(/\d+/)[0], 10),
        unit = obj.dose.replace(/\d+/, '').trim();
    result[unit] = result[unit] ? result[unit] + quantity : quantity;
});

var arr = Object.keys(result).map(key => result[key] + ' ' + key);
console.log(arr);

答案 1 :(得分:0)

不需要jQuery,只需使用JSON.parse:

var response; // This is your json string from server.
var responseObject = JSON.parse(response);
// Get the keys with responseObject[i].dose, where i = the index in the array, i.e:
console.log(responseObject[0].dose); // prints "2 acs"

答案 2 :(得分:0)

您可以使用以下代码。希望这会对你有所帮助。

var list = [
 {
   "product": "CLINISod",
   "potentiation": "BIOLOGICALLY ACTIVE",
   "dose": "2 acs",
   "period": "all",
   "in":"Morning"
 },      
 {
  "product": "CLINISod",
  "potentiation": "BIOLOGICALLY ACTIVE",
  "dose": "1 acs",
  "period": "all",
  "in":"Evening"
 },      
 {
  "product": "CLINISod",
  "potentiation": "BIOLOGICALLY ACTIVE",
  "dose": "2 cs",
  "period": "all",
  "in":"Morning"
 },
 {
  "product": "CLINISod",
  "potentiation": "BIOLOGICALLY ACTIVE",
  "dose": "1 cs",
  "period": "all",
  "in":"Evening"
 }
 ];



var acsSum = 0;
var csSum = 0;

$.each(Object.keys(list), function(index,value){

if(list[index].dose.indexOf("acs") != -1)
{

  acsSum = acsSum + Number(list[index].dose.split(" ")[0]);
}
else
{
  csSum = csSum + Number(list[index].dose.split(" ")[0]);
}

});
console.log(acsSum + " acs");
console.log(csSum + " cs");

console.log-1:3 acs的结果 console.log-2:3 cs

的结果

答案 3 :(得分:0)

var rawData = [{
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 acs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 acs",
    "period": "all",
    "in": "Evening"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "2 cs",
    "period": "all",
    "in": "Morning"
}, {
    "product": "CLINISod",
    "potentiation": "BIOLOGICALLY ACTIVE",
    "dose": "1 cs",
    "period": "all",
    "in": "Evening"
}];

var objectHash = {};
rawData.forEach(function(product) {
  var breakdown = product.dose.split(' ');
  var unit = breakdown[1];
  var quantity = Number(breakdown[0]);
  objectHash[unit] = objectHash[unit] || 0;
  objectHash[unit] += quantity;
});

function converToArray(obj) {
  return Object.keys(obj).map(function(key) {
    return obj[key] + ' ' + key;
  })
}

var results = converToArray(objectHash);
console.log(results);

相关问题