Javascript AngularJs中的数组映射

Javascript AngularJs中的数组映射,javascript,angularjs,arrays,Javascript,Angularjs,Arrays,我有两个数组 $scope.tags = [{ "id": 1, "name": "python" }, { "id": 2, "name": "NodeJs" }, { "id": 3, "name": "git" }] 另一个是 $scope.skillsInterested = [1,2]; 你想做什么 如何映射上述数组并仅打印$scope.skillsInterested 我想打印第一个数组中的名称,第二个数组中只显示id 在得到几个答案后,我尝试了这个方法 var tag_map

我有两个数组

$scope.tags =  [{ "id": 1, "name": "python" }, { "id": 2, "name": "NodeJs" }, { "id": 3, "name": "git" }]
另一个是

$scope.skillsInterested = [1,2];
你想做什么

如何映射上述数组并仅打印
$scope.skillsInterested

我想打印第一个数组中的名称,第二个数组中只显示id

在得到几个答案后,我尝试了这个方法

var tag_map = {};
for (var x = 0; x < $scope.tags.length; x++) {
tag_map[$scope.tags[x]['id']] = $scope.tags[x]['name'];
}
$scope.skillsInts = $scope.skillsInterested.map(function(x) {
return tag_map[x]
它有时给出结果,有时给出未定义的“映射”

TypeError: Cannot read property 'map' of undefined
at controllers.js:141
at angular.js:16383
at m.$eval (angular.js:17682)
at m.$digest (angular.js:17495)
at m.$apply (angular.js:17790)
at l (angular.js:11831)
at J (angular.js:12033)
at XMLHttpRequest.t.onload (angular.js:11966)

提前谢谢

首先对
使用过滤功能,然后检查id的存在,然后从数组中映射名称

var first=[{“id”:1,“name”:“python”},{“id”:2,“name”:“NodeJs”},{“id”:3,“name”:“git”}];
var selectedExpTags=[1,2];
var name=first.filter(item=>selectedExpTags.some(id=>item.id==id)).map(item=>item.name);

console.log(名称)制作一张数据地图,如下所示:

var tagMap = { 1: "python", 2: "NodeJs" /* etc. */ };
可以通过在标记上循环并向对象添加新属性来实现这一点
reduce
允许您在不创建任何额外变量的情况下执行此操作

然后,您可以使用
[]
符号从新创建的对象中选择名称:
tagMap[1]
返回
“pyhton”

var tags=[{“id”:1,“name”:“python”},{“id”:2,“name”:“NodeJs”},{“id”:3,“name”:“git”}]
var selectedExpTags=[1,2];
//为'id:name'制作一张地图`
var tagMap=tags.reduce(函数(map,tag){
map[tag.id]=tag.name;
返回图;
}, {});
//从地图中快速选择名称:
var selectedNames=selectedExpTags.map(函数(id){
返回标记映射[id];
});

console.log(selectedNames)您可以循环使用
$scope。选择Exptags
并获取所有名称的列表。您可以使用
数组。如果只需要第一个值,请查找

样品
var first=[
{“id”:1,“name”:“python”},
{“id”:2,“name”:“NodeJs”},
{“id”:3,“name”:“git”}];
var selectedExpTags=[1,2];
var name=selectedExpTags.map(x=>first.find(y=>y.id==x).name)
console.log(名称)

Lodash在处理数据方面比angular更有效。

向我们展示您的
forEach
以及您得到的“仅打印”错误意味着您要筛选第一个数组-
$scope.tags.filter(x=>x.id===$scope.selectedExpTags)[0]。name
$scope.tags.find(x=====$scope.selectedExpTags).name
首先。where不是函数“
有时它给出结果,有时它抛出未定义的映射SelectedExptags必须是数组它是数组请检查我所做的编辑,我尝试过simple for循环,有时它给出结果,有时它抛出错误
无法读取未定义的属性“map”
意味着您有一个
未定义的变量,并且您试图访问它上的
.map
。例如:
var a=未定义;a、 map()所以我会尝试这样的方法:
$scope.skillsInts=$scope.skillsInterested$scope.skillsInterested.map(/*etc*/):[]。当未定义
skillsInterested
时,这会将
skillsInts
设置为空数组。
var tagMap = { 1: "python", 2: "NodeJs" /* etc. */ };
$scope.newArray = []; // If you need a new array to work with
angular.forEach($scope.tags, function(tag){
$scope.selectedExpTags.forEach(function(selectedTag){ 
    if(selectedTag == tag.id){
        //tag.hide = false; // - If you want to update the current array
        $scope.newArray.push(tag);
    }
    // else{ // - If you want to update the current array
    //      tag.hide = true;
    // }
  })
})