通过使用Javascript操纵现有JSON对象来创建JSON对象

时间:2016-01-28 08:42:13

标签: javascript json

我有一个JSON对象,我正在操作数据并尝试构造一个新的JSON对象。问题是我无法找到创建JSON对象的动态部分。这是实际的JSON对象

[{"acptFlag":true,"count":14288,"limsFlag":true,"plantId":30,"plantName":"Camilla, GA","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":false,"count":344,"limsFlag":true,"plantId":30,"plantName":"Camilla, GA","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":false,"count":700,"limsFlag":true,"plantId":31,"plantName":"Albany, KY","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":true,"count":9500,"limsFlag":true,"plantId":31,"plantName":"Albany, KY","supplierId":20,"supplierName":"Keystone Foods"},{"acptFlag":false,"count":227,"limsFlag":true,"plantId":32,"plantName":"Green Forest, AR","supplierId":21,"supplierName":"Tyson Foods"},{"acptFlag":true,"count":7049,"limsFlag":true,"plantId":32,"plantName":"Green Forest, AR","supplierId":21,"supplierName":"Tyson Foods"},{"acptFlag":true,"count":10742,"limsFlag":true,"plantId":33,"plantName":"Dawson, GA","supplierId":21,"supplierName":"Tyson Foods"},{"acptFlag":false,"count":506,"limsFlag":true,"plantId":33,"plantName":"Dawson, GA","supplierId":21,"supplierName":"Tyson Foods"}]

所需的JSON对象是

[{"supplierName":"Keystone Foods","1":(344/(344+14288)) , "2":700/(700+9500))}},{"supplierName":"Tyson Foods"","1":(227/(227+7049)) , "2":(506/(506+10742))}}]

这里我试图将“Keystone Foods”数据放在一个对象中。 "1":(344/(344+14288)) , "2":700/(700+9500))表示错误计数/真实计数。 “1”和“2”代表植物的数量。我陷入了构建工厂计数代码的困境。

这是我现有的代码。

var resultJSON = {}, arr = [];
var jsonObj = JSON.parse(obj)
for (var key in jsonObj) {
  if (jsonObj.hasOwnProperty(key)) {
    var val = jsonObj[key];
    var supplierName = val.supplierName;
    arr.push(supplierName);    
  }
}
var M = {}, R = []
getUniqueSuppliers(arr)
function getUniqueSuppliers(arr) {
   for(var i = 0; i < arr.length; i++) {
      M[arr[i]] = "supplierName"
   }
  for(var i in M) {
    R.push(i)
  }
}
checkEachSupplier(R)
function checkEachSupplier(R) { 
    for (var index = 0; index < R.length; index++) {
       var supplierName = R[index]
       createNewJSON(supplierName)
   }
}
getPlantTotalValueBasedOnSupplier(R);
function createNewJSON(supplierName) {

    for (var key in jsonObj) {
       if (jsonObj.hasOwnProperty(key)) {
       var val = jsonObj[key];    
       if(supplierName == val.supplierName) {       
         resultJSON[supplierName] = {"supplierName" : supplierName}
       }
     }
   }
 }
 console.log(resultJSON)

我的JSON小提琴: https://jsfiddle.net/7u5qphx9/6/

1 个答案:

答案 0 :(得分:0)

也许这有帮助。

它为temp使用临时对象count

{
    "Keystone Foods": {
        "30": {
            "true": 14288,
            "false": 344
        },
        "31": {
            "false": 700,
            "true": 9500
        }
    },
    "Tyson Foods": {
        "32": {
            "false": 227,
            "true": 7049
        },
        "33": {
            "true": 10742,
            "false": 506
        }
    }
}

部分代码:

temp = obj1.reduce(function (r, a) {
    // ...
    return r;
}, {}),

Array.prototype.reduce()遍历obj1并将空对象作为起始值。回调函数返回下一个迭代步骤的对象r,或者结果返回结果。

r[a.supplierName] = r[a.supplierName] || {};

如果有r[a.supplierName]的值,则为的简写,而不是r[a.supplierName],否则请创建新对象

{
    "Keystone Foods": {}
}
r[a.supplierName][a.plantId] = r[a.supplierName][a.plantId] || {};

此处属性plantID也是如此。如果没有值,则创建一个新对象。

{
    "Keystone Foods": {
        "30": {}
    }
}
r[a.supplierName][a.plantId][a.acptFlag] = a.count;

这会将count分配给值a.acptFlag

的属性
{
    "Keystone Foods": {
        "30": {
            "true": 14288
    },
}

这是对象。现在我看一下这个部分,用于构建一个包含所需对象o的新数组。

obj2 = Object.keys(temp).map(function (k) {
    // ...
    return o;
});

首先从temp获取所有keys并使用Array.prototype.map()进行迭代,因为这应该构建一个新数组。

回调函数内部

var o = { supplierName: k };

使用属性supplierName和实际键的值构建一个新对象。

Object.keys(temp[k]).forEach(function (kk, i) {
    // ...
});

现在需要迭代plantId的下一级密钥。

o[i + 1] = '' + temp[k][kk].false + ' / (' + temp[k][kk].false + ' + ' + temp[k][kk].true + ')';

对于每个plantID,使用true和false属性的值创建一个新属性。指定的值是一个字符串。 (如果该行更改为o[i + 1] = temp[k][kk].false / (temp[k][kk].false + temp[k][kk].true);,则可以是数字。)

var obj1 = [{ "acptFlag": true, "count": 14288, "limsFlag": true, "plantId": 30, "plantName": "Camilla, GA", "supplierId": 20, "supplierName": "Keystone Foods" }, { "acptFlag": false, "count": 344, "limsFlag": true, "plantId": 30, "plantName": "Camilla, GA", "supplierId": 20, "supplierName": "Keystone Foods" }, { "acptFlag": false, "count": 700, "limsFlag": true, "plantId": 31, "plantName": "Albany, KY", "supplierId": 20, "supplierName": "Keystone Foods" }, { "acptFlag": true, "count": 9500, "limsFlag": true, "plantId": 31, "plantName": "Albany, KY", "supplierId": 20, "supplierName": "Keystone Foods" }, { "acptFlag": false, "count": 227, "limsFlag": true, "plantId": 32, "plantName": "Green Forest, AR", "supplierId": 21, "supplierName": "Tyson Foods" }, { "acptFlag": true, "count": 7049, "limsFlag": true, "plantId": 32, "plantName": "Green Forest, AR", "supplierId": 21, "supplierName": "Tyson Foods" }, { "acptFlag": true, "count": 10742, "limsFlag": true, "plantId": 33, "plantName": "Dawson, GA", "supplierId": 21, "supplierName": "Tyson Foods" }, { "acptFlag": false, "count": 506, "limsFlag": true, "plantId": 33, "plantName": "Dawson, GA", "supplierId": 21, "supplierName": "Tyson Foods" }],
    temp = obj1.reduce(function (r, a) {
        r[a.supplierName] = r[a.supplierName] || {};
        r[a.supplierName][a.plantId] = r[a.supplierName][a.plantId] || {};
        r[a.supplierName][a.plantId][a.acptFlag] = a.count;
        return r;
    }, {}),
    obj2 = Object.keys(temp).map(function (k) {
        var o = { supplierName: k };
        Object.keys(temp[k]).forEach(function (kk, i) {
            o[i + 1] = '' + temp[k][kk].false + ' / (' + temp[k][kk].false + ' + ' + temp[k][kk].true + ')';
        });
        return o;
    });

document.write('<pre>' + JSON.stringify(temp, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(obj2, 0, 4) + '</pre>');