admin管理员组

文章数量:1022964

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

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

本文标签: javascriptHow to filter JSON data by properties that contain certain stringsStack Overflow