我的数组:从一个数组创建JSON和对象
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
等。
so ["low"]
与values.abc
中的值匹配。
b)现在,我需要检查没有元件在阵列中的["low"]
情况下,它只是一个
c)现在我您在values.abc
(这里3的最小数字键),并将其匹配至获取对象的阵列{"name":"low","value":"a"}
d)下一个阵列["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)类似的下一个阵列具有长度为4,并将其匹配的的数字键4中的值values.xyz
,我们生成四个对象.. {"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
}
@downvoters,是什么在这个问题上是如此糟糕?我认为,OP的目标是明确的 – RomanPerekhrest
@RomanPerekhrest我只是不明白它..是否downvoting provde指向选民,这是必要的,downvote问题的人至少添加评论,他们的理由downvote – Arnab
是的,这是一个可悲的事实。 *“只有一个人不能做任何事......”* – RomanPerekhrest