0

I have a JSON Object that looks like this:

{
  'name': 'Bob',
  'friends': [
    {
      'name' : 'Ashley (Family)'
    },
    {
      'name' : 'Steven (Non-Family)'
    },
    {
      'name' : 'Chris (Family)'
    }
  ]
}

How can I filter the above, so that it returns only the friends that are family? i.e. friends who's name contains '(Family)'?

function filterFriends (friends) {

  return friends.filter(function(i) {
    if (i.name.indexOf('(Family)') > -1) {
      return i.name;
    }
  });

}

But the above doesn't seem to work... I don't know if I'm on the right track?

4
  • replace return i.name with return true Commented Oct 30, 2015 at 7:51
  • You say it doesn't seem to work, what are you seeing? Commented Oct 30, 2015 at 7:53
  • 1
    seems to be working fine Commented Oct 30, 2015 at 7:55
  • 1
    Please explain why you think it " doesn't seem to work". Voting to close since it is not clear what the problem is. Commented Oct 30, 2015 at 8:07

2 Answers 2

4

Other than a) using the phrase "JSON Object" which makes no sense and b) relying on sloppy automatic casting of booleans, you really don't have a problem. This "answer", with minor technical improvements will demonstrate that your code is just fine.

var data = {
  name: 'Bob',
  friends: [
    {
      name: 'Ashley (Family)'
    },
    {
      name: 'Steven (Non-Family)'
    },
    {
      name: 'Chris (Family)'
    }
  ]
};

var family = data.friends.filter(f => f.name.indexOf('(Family)') > -1);

console.log(family);
// [{name: 'Ashley (Family)'}, {name: 'Chris (Family)'}]

If you want to write it into a function

function isFamily(name) {
  return name.indexOf('(Family)') > -1;
}

function getFamily(friends) {
  return friends.filter(f => isFamily(f.name));
}

var family = getFamily(data.friends);

ES5

var family = data.friends.filter(function(f) {
  return f.name.indexOf('(Family)') > -1);
});

console.log(family);
Sign up to request clarification or add additional context in comments.

7 Comments

Your way is exactly the same as OP's way, did you try to understand his problem?
@Motti sorry but you don't know what the word "exact" means.
@TJ if by "same" you mean my code also uses an if and relies upon strings being automatically cast to true and undefined being cast to false, then sure, the answers are the "same" - I don't see either though.
They are semantically equivalent, you use true and false while he uses truthy and falsely and you use a different syntax for lambda functions. But in essence they are the same
@maček people like you are promoting crappy questions trying to be the fastest gun without even checking whether there is a problem. As a reputed member, if you think there is no problem you should comment the same and close the question rather than trying to farm rep from everything you see
|
0

Filter method should always return boolean value, this looks like returning always the string with the name.

Take a look to docs for .filter method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

3 Comments

It doesn't have to. JavaScript truthy/falsy will be just fine.
That's false, only on his condition is he returning true, if it fails then the function returns with undefined, which is false.
@Konrad, I agree with you that it should always return a boolean. Relying on truthiness is sloppy, in my opinion.

Your Answer

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