2016-07-04 110 views
-3

我的数组:从一个数组创建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 
} 
+1

@downvoters,是什么在这个问题上是如此糟糕?我认为,OP的目标是明确的 – RomanPerekhrest

+0

@RomanPerekhrest我只是不明白它..是否downvoting provde指向选民,这是必要的,downvote问题的人至少添加评论,他们的理由downvote – Arnab

+0

是的,这是一个可悲的事实。 *“只有一个人不能做任何事......”* – RomanPerekhrest

回答

1

使用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));

1

你可以只遍历并收集想要的部分。

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);