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
withreturn 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
2 Answers
Reset to default 4Other 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
withreturn 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
2 Answers
Reset to default 4Other 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
版权声明:本文标题:javascript - How to filter JSON data by properties that contain certain strings? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745594924a2158107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论