0

So my data-set looks like this (much more stats behind it but lets keep this simple):

$scope.peopleData = [
  {"name": "Name1", "img": "name1Img.png"},
  {"name": "Name2", "img": "name2Img.png"}...
];

Doing just fine displaying all of the items via a simple:

<div ng-repeat="person in peopleData">
  <img src="img/people/{{person.img}}" alt="{{person.name}}" />
</div>

Now let's say I'm storing friends and have a different section that only lists specific people. Take, for example, an array such as:

var friends = ["Name4", "Name9"];

How would I go about writing a function that would return:

$scope.friends = [
  {"name": "Name4", "img": "name4Img.png"},
  {"name": "Name9", "img": "name9Img.png"}...
];

Essentially, I want to search through all of the peopleData and return a smaller array based upon several values contained within an array?

Surely there has to be a simple method to use .filter() to find multiple strings as acceptable values?

$scope.friendData = peopleData.filter(friends);

I think I'm just missing a small piece here...

3
  • 1
    var friends = ["Name4", "Name9"];. There is no space before digit ? Commented Jun 14, 2016 at 7:47
  • Have you tried using filter with indexOf? Commented Jun 14, 2016 at 7:48
  • @Rayon There will be no spaces in the fields. Sorry for the quick typeup. Editing... Commented Jun 14, 2016 at 7:49

3 Answers 3

2

Use Array#filter with Array#indeOf to test the value in array

var friends = [{
  "name": "Name4",
  "img": "name4Img.png"
}, {
  "name": "Name9",
  "img": "name9Img.png"
}, {
  "name": "Name10",
  "img": "name10Img.png"
}];
var friendsArr = ["Name4", "Name9"];
var friendData = friends.filter(function(item) {
  return friendsArr.indexOf(item.name) !== -1; //`item.name` exists in `friendsArr`
});
console.log(friendData);

Sign up to request clarification or add additional context in comments.

Comments

0
function getFriendsDetails()
{
    var friends = ["Name4", "Name9"];
    var friendsDetails  = [];

    for(var i = 0; i<friends.length;i++)
    {
       var friend = friends[i];
       friendsDetails.push({"name":friend, "img":(friend.toLowerCase()+"Img.png")});
    }

    return friendsDetails;
}

Example Fiddle

Comments

0

For a greater data amount, you could use a hash table for faster lookup

var $scope = {},
    friends = ["Name4", "Name9"];

$scope.peopleData = [{ "name": "Name1", "img": "name1Img.png" }, { "name": "Name4", "img": "name4Img.png" }],

$scope.friendData = $scope.peopleData.filter(function (friends) {
    var hash = Object.create(null);
    friends.forEach(function (a) { hash[a] = true; });
    return function (a) { return hash[a.name]; };
}(friends));

console.log($scope.friendData);

In ES6 you could filter it with Array.includes.

$scope.friendData = peopleData.filter(a => friends.includes(a.name));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.