这是我的静态值,其ID为:
var staticValues = [
{ "id": "1", "name": "PEKKA" },
{ "id": "2", "name": "Golem" },
{ "id": "3", "name": "Vigilane" },
{ "id": "4", "name": "SpiderMan" },
{ "id": "5", "name": "Archer" },
{ "id": "6", "name": "SuperMan" }
]
这是我从我的方法返回的值:
var myReturnedValues = [
[ [ "2", "4" ] ],
[ [ "5", "5" ] ],
[ [ "1", "3" ] ],
[ [ "4", "3" ] ]
]
我的输出必须是这样的:
var myReturnedValues = [
[ [ "Golem", "SpiderMan" ] ],
[ [ "Archer", "Archer" ] ],
[ [ "PEKKA", "Vigilante" ] ],
[ [ "SpiderMan", "Vigilante" ] ]
]
我在这里尝试做的是我必须比较staticValues
和myReturnedValues
并返回与其ID相关的名称,而不是仅返回id。我用underscore.js尝试了一些方法,但失败了。有人能给我这个想法吗?
这就是我的方法的样子:
var staticValues = [ /* here I have the whole static data */ ];
$scope.getCategories = function() {
var myReturnedValues = mainSteps.map(x => [x.steps.map(y => y.category)]);
return myReturnedValues;
}
代码编辑后,
$scope.getCategories =function(){
var myReturnedValues =mainSteps.map(x => [x.steps.map(y => y.category+"\n"+"\n"+"\n")]);
//return myReturnedValues;
console.log('meeee before',angular.toJson(myReturnedValues));
newval = {};
$.each(staticValues ,function(i,v) {
console.log('meeee staticCategories',angular.toJson(staticValues ));
newval[v.id] = v.name;
});
$.each(myReturnedValues,function(i,v){
$.each(v[0],function(x,t){
myReturnedValues[i][0][x] = newval[t];
});
});
console.log('meeee after',angular.toJson(myReturnedValues));
return myReturnedValues;
}
答案 0 :(得分:2)
首先将staticValues
转换为key => value
对象:
names = {}
staticValues.forEach(obj => names[obj.id] = obj.name);
然后迭代myReturedValues
并用你的名字替换id:
var staticValues = [
{ "id": "1", "name": "PEKKA" },
{ "id": "2", "name": "Golem" },
{ "id": "3", "name": "Vigilane" },
{ "id": "4", "name": "SpiderMan" },
{ "id": "5", "name": "Archer" },
{ "id": "6", "name": "SuperMan" }
]
var myReturnedValues = [
[ [ "2", "4" ] ],
[ [ "5", "5" ] ],
[ [ "1", "3" ] ],
[ [ "4", "3" ] ]
]
names = {}
staticValues.forEach(x => names[x.id] = x.name)
res = myReturnedValues.map(sub =>
[sub[0].map(id => names[id])]
)
console.log(res)

答案 1 :(得分:1)
尝试以下循环:
newval = {};
$.each(staticValues,function(i,v) {
newval[v.id] = v.name;
});
var myReturedValues = [
[ [ "2", "4" ] ],
[ [ "5", "5" ] ],
[ [ "1", "3" ] ],
[ [ "4", "3" ] ]
];
$.each(myReturedValues,function(i,v){
$.each(v[0],function(x,t){
myReturedValues[i][0][x] = newval[t];
});
});
答案 2 :(得分:1)
使用下划线,您可以尝试这样:
var staticValues = [
{ "id": "1", "name": "PEKKA" },
{ "id": "2", "name": "Golem" },
{ "id": "3", "name": "Vigilane" },
{ "id": "4", "name": "SpiderMan" },
{ "id": "5", "name": "Archer" },
{ "id": "6", "name": "SuperMan" }
];
var myReturedValues = [
[ [ "2", "4" ] ],
[ [ "5", "5" ] ],
[ [ "1", "3" ] ],
[ [ "4", "3" ] ]
];
var data = _.chain(myReturedValues).map(function(d) {
//map the myReturedValues
return d[0].map(function(id) {
//use underscore to search in the staticValues and return name
return _.find(staticValues, function(svalue) {
return svalue.id == id
}).name
});
}).value();
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>