最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to filter JSON data by properties that contain certain strings? - Stack Overflow

matteradmin7PV0评论

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?

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?

Share Improve this question asked Oct 30, 2015 at 7:46 user818700user818700 4
  • replace return i.name with return true – m02ph3u5 Commented Oct 30, 2015 at 7:51
  • You say it doesn't seem to work, what are you seeing? – Motti Commented Oct 30, 2015 at 7:53
  • 1 seems to be working fine – T J 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. – T J Commented Oct 30, 2015 at 8:07
Add a ment  | 

2 Answers 2

Reset to default 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);

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/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Post a comment

comment list (0)

  1. No comments so far