我正在寻找最好的,最小的操作来解决这个问题。
var sourceDictionary = {
"200" : [
[ "a", 5 ],
[ "al", 6 ],
[ "xl", 8 ]
],
"201" : [
[ "b", 2 ],
[ "al", 16 ],
[ "al", 26 ],
[ "al", 9 ],
[ "al", 3 ]
],
"202" : [
[ "lm", 7 ]
]
}
我想根据每个键中包含的整数值对字典进行排序,然后对每个值进行排序,如outputputDictionary所示。
var targetDictionary = {
"200" : [
[ "a", 5, "rank-7" ],
[ "al", 6, "rank-6" ],
[ "xl", 8, "rank-4" ]
],
"201" : [
[ "b", 2, "rank-9" ],
[ "al", 16, , "rank-2" ],
[ "al", 26, "rank-1" ],
[ "al", 9, "rank-3" ],
[ "al", 3, "rank-8" ]
],
"202" : [
[ "lm", 7, "rank-5" ]
]
}
例如[ "al", 26, "rank-1" ]
。这是排名-1,因为26是所有其他值中的最大值。
Javascript是最优选的语言。寻找最佳的最佳解决方案
答案 0 :(得分:3)
由于数组是通过引用传递的,因此您可以像这样使用它:
function rankify(obj) {
// PHASE 1: get a reference of all the sub-arrays
var references = [];
for(var key in obj) { // for each key in the object obj
obj[key].forEach(function(e) { // for each element e (sub-array) of the array obj[key]
references.push(e); // push a reference of that array into reference array
});
}
// PHASE 2: sort the references
references.sort(function(a, b) { // sort the items
return b[1] - a[1]; // to reverse the sort order (a[1] - b[1])
});
// PHASE 3: assign the ranks
references.forEach(function(e, i) { // for each array in the reference array
e.push("rank-" + (i + 1)); // push another item ("rank-position") where the position is defined by the sort above
});
}
var sourceDictionary = {"200" : [[ "a", 5 ],[ "al", 6 ],[ "xl", 8 ]],"201" : [[ "b", 2 ],[ "al", 16 ],[ "al", 26 ],[ "al", 9 ],[ "al", 3 ]],"202" : [[ "lm", 7 ]]};
rankify(sourceDictionary);
console.log(sourceDictionary);
如果您被允许使用箭头功能:
function rankify(obj) {
Object.keys(obj)
.reduce((ref, k) => ref.concat(obj[k]), []) // get the references array
.sort((a, b) => b[1] - a[1]) // sort it
.forEach((e, i) => e.push("rank-" + (i + 1))); // assign the rank
}
var sourceDictionary = {"200" : [[ "a", 5 ],[ "al", 6 ],[ "xl", 8 ]],"201" : [[ "b", 2 ],[ "al", 16 ],[ "al", 26 ],[ "al", 9 ],[ "al", 3 ]],"202" : [[ "lm", 7 ]]};
rankify(sourceDictionary);
console.log(sourceDictionary);
答案 1 :(得分:0)
这可以通过几行来完成:
var sourceDictionary = {
"200" : [
[ "a", 5 ],
[ "al", 6 ],
[ "xl", 8 ]
],
"201" : [
[ "b", 2 ],
[ "al", 16 ],
[ "al", 26 ],
[ "al", 9 ],
[ "al", 3 ]
],
"202" : [
[ "lm", 7 ]
]
}
var flatten = arr => [].concat.apply([], arr)
var ranks = flatten(Object.keys(sourceDictionary)
.map(k => sourceDictionary[k].map(t => t[1]))
)
.sort((a, b) => b - a)
.filter( function( item, index, inputArray ) {
// remove duplicates
return inputArray.indexOf(item) == index;
});
Object.keys(sourceDictionary)
.forEach(k => sourceDictionary[k]
.forEach(t => t.push("rank-" + (1 + ranks.indexOf(t[1])))))
console.log(sourceDictionary)

答案 2 :(得分:0)
您可以先将其缩小为从原始对象存储key|index
的数组,然后对其进行排序并添加rank属性,然后再次创建对象。
var data = {
"200" : [ [ "a", 5 ], [ "al", 6 ], [ "xl", 8 ] ],
"201" : [ [ "b", 2 ], [ "al", 16 ], [ "al", 26 ], [ "al", 9 ], [ "al", 3 ] ],
"202" : [ [ "lm", 7 ] ]
}
var o = Object.keys(data).reduce(function(r, e) {
data[e].forEach((a, i) => r.push([e + '|' + i, a]))
return r;
}, [])
o.sort((a, b) => b[1][1] - a[1][1]).map(function(e, i) {
e[1][2] = 'rank-' + (i + 1)
})
var result = o.reduce(function(r, e) {
var key = e[0].split('|')
if (!r[key[0]]) r[key[0]] = []
r[key[0]][key[1]] = e[1]
return r
}, {})
console.log(result)