I am dealing with two arrays
$scope.tags = [{ "id": 1, "name": "python" }, { "id": 2, "name": "NodeJs" }, { "id": 3, "name": "git" }]
The second array is
$scope.skillsInterested = [1,2];
What I am trying to achieve is:
How do I match the IDs in$scope.skillsInterested
array with the first array and print only their names?
I want to display names from the first array for the IDs present in the second.
After receiving multiple suggestions, here's what I attempted:
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]
When I use console.log
console.log("Result", tag_map);
Sometimes it returns a result, but other times it shows 'map' of undefined.
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)
Thank you in advance.