繁体   English   中英

比较JavaScript中的两个对象数组

[英]Comparing two array of objects in javascript

这是我的静态值,其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" ] ]
]

在这里要做的是我必须比较staticValuesmyReturnedValues并返回有关其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; } 

首先将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) 

尝试以下循环:

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

演示: https : //jsfiddle.net/cgk478g8/

使用下划线,您可以这样尝试:

 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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM