从数组和对象创建json

creating json from an array and an object

本文关键字:创建 json 对象 数组      更新时间:2023-09-26

My array:

var categories =  [
    ["low"],
    ["medium low", "medium medium", "medium high"],
    ["high low", "high medium", "high high", "high obscene"]
 ];
我对象:

var values = {
  abc: {
    3: ["a", "b", "c"],
    4: ["m", "n", "o", "p"].
    5: ["v", "w", "x", "y", "z"]
  },
  def: {
    3: ["a1", "b2", "c1"],
    4: ["m7", "n5", "o8", "p3"].
    5: ["v6", "w7", "x4", "y5", "z9"]
  },
  xyz: {
    3: ["a3", "b4", "c6"],
    4: ["m6", "n3", "o7", "p1"].
    5: ["v3", "w9", "x2", "y7", "z8"]
  }
}

这就是我想做的。

a)对于categories数组中的每个数组,我将其与values对象进行匹配,以便categories中的第一个数组与values中的第一个对象匹配,以此类推。

因此["low"]values.abc中的值相匹配。

b)现在,我需要检查数组中元素的数目在["low"]的情况下,它只有一个

c)现在我检查values.abc中的最小数字键(这里是3)并将其与数组匹配以获得对象{"name":"low","value":"a"}

d)next array ["medium low", "medium medium", "medium high"]匹配values.def中的值。这里,数组的长度是3,并将其与最小数字键匹配,我们得到3,并生成三个对象。{"name":"medium low","value":"a1"},{"name":"medium medium","value":"b2"},{"name":"medium high","value":"c1"}

e)同样,next数组的长度为4,并将其与values.xyz的数字键4的值进行匹配,我们生成四个对象。{"name":"high low","value":"m6"},{"name":"high medium","value":"n3"},{"name":"high high","value":"o7"},{"name":"high obscene","value":"p1"}

所以结果应该是:

var results = [{
  "name": "low",
  "value": "a"
}, {
  "name": "medium low",
  "value": "a1"
}, {
  "name": "medium medium",
  "value": "b2"
}, {
  "name": "medium high",
  "value": "c1"
}, {
  "name": "high low",
  "value": "m6"
}, {
  "name": "high medium",
  "value": "n3"
}, {
  "name": "high high",
  "value": "o7"
}, {
  "name": "high obscene",
  "value": "p1"
}]

我能弄明白的很少。

 var results = categories.forEach(myFunction);
function myFunction(item, index) {
    var l = item.length;
//I need to figure out based on index get the reqd object in values
// Also based on above var l, get the right array against the numeric key 
}

使用Object.keysArray.forEachArray.indexOf函数的解决方案:

var categories =  [
    ["low"],
    ["medium low", "medium medium", "medium high"],
    ["high low", "high medium", "high high", "high obscene"]
 ];
 
 var values = {
  abc: {
    3: ["a", "b", "c"],
    4: ["m", "n", "o", "p"],
    5: ["v", "w", "x", "y", "z"]
  },
  def: {
    3: ["a1", "b2", "c1"],
    4: ["m7", "n5", "o8", "p3"],
    5: ["v6", "w7", "x4", "y5", "z9"]
  },
  xyz: {
    3: ["a3", "b4", "c6"],
    4: ["m6", "n3", "o7", "p1"],
    5: ["v3", "w9", "x2", "y7", "z8"]
  }
};
var valuesKeys = Object.keys(values), 
    result = [];
categories.forEach(function (v, k) {
    var size = String(v.length),
        inner_obj = values[valuesKeys[k]],
        inner_keys = Object.keys(inner_obj),
        matches_item = (inner_keys.indexOf(size) !== -1)? inner_obj[size] : inner_obj[Math.min.apply(null, inner_keys)];
        
    v.forEach(function (name, k) {
        result.push({name: name, value: matches_item[k]});    
    });
})
console.log(JSON.stringify(result, 0, 4));

你可以遍历并收集所需的部件。

var categories = [["low"], ["medium low", "medium medium", "medium high"], ["high low", "high medium", "high high", "high obscene"]],
    values = { abc: { 3: ["a", "b", "c"], 4: ["m", "n", "o", "p"], 5: ["v", "w", "x", "y", "z"] }, def: { 3: ["a1", "b2", "c1"], 4: ["m7", "n5", "o8", "p3"], 5: ["v6", "w7", "x4", "y5", "z9"] }, xyz: { 3: ["a3", "b4", "c6"], 4: ["m6", "n3", "o7", "p1"], 5: ["v3", "w9", "x2", "y7", "z8"] } },
    order = Object.keys(values).sort(),
    result = categories.reduce(function (r, a, i) {
        var keys = Object.keys(values[order[i]]).sort(function (a, b) { return a - b; });
        return r.concat(a.map(function (b, j) {
            return {
                name: b,
                value: (values[order[i]][a.length] || values[order[i]][keys[0]])[j]
            };
        }));
    }, []);
console.log(result);