admin管理员组文章数量:1410706
I have array of objects. I want to find the value in key which matches the word and if it's there it should return me true in console otherwise false.
I have array of objects like this:
[
{
"groupName": "harry",
},
{
"groupName": "mike",
}
]
And I want to match this key groupName
with value like Amy
. As we can see Amy
in not there in groupName
so it should return me false in console otherwise true if any of the groupName
value is Amy
.
How to achieve this using Javascript?
I have array of objects. I want to find the value in key which matches the word and if it's there it should return me true in console otherwise false.
I have array of objects like this:
[
{
"groupName": "harry",
},
{
"groupName": "mike",
}
]
And I want to match this key groupName
with value like Amy
. As we can see Amy
in not there in groupName
so it should return me false in console otherwise true if any of the groupName
value is Amy
.
How to achieve this using Javascript?
Share edited May 10, 2021 at 20:19 progpro asked May 10, 2021 at 19:16 progproprogpro 1956 silver badges21 bronze badges 4- If you want it in the console, what do you want react to do? – Charlie Bamford Commented May 10, 2021 at 19:17
-
If it returning true then I have to use
useState
to fire up a action. Otherwise if it is false then I would show alert in that. I just need the concept of matching it with the given word. That's it. Otherwise it would be lengthy question and answer to be asked. – progpro Commented May 10, 2021 at 19:20 - Does this answer your question? How to determine if Javascript array contains an object with an attribute that equals a given value? – Henry Ecker ♦ Commented May 10, 2021 at 19:20
- Specifically this answer – Henry Ecker ♦ Commented May 10, 2021 at 19:20
3 Answers
Reset to default 3You can simply use Array.some()
:
const data = [{
"groupName": "harry",
},
{
"groupName": "mike",
}
];
const result = data.some(obj => obj.groupName === 'harry');
console.log(result);
Or Array.find()
to get the matching object:
const data = [{
"groupName": "harry",
},
{
"groupName": "mike",
}
];
const result = data.find(obj => obj.groupName === 'harry');
console.log(result);
You could use this code (functional code style):
const input = [
{
"groupName": "harry",
},
{
"groupName": "mike",
}
];
const findByPropertyName = name => arr => searched => arr.some(({ [name]: value}) => value === searched);
const findByGroupName = findByPropertyName('groupName');
const notFound = findByGroupName(input)('Amy');
const found = findByGroupName(input)('harry');
console.log(notFound);
console.log(found);
React has nothing to do with what you're trying to achieve.
With just Javascript, you can do this using the some Array method like this.
const obj = [
{
"groupName": "harry",
},
{
"groupName": "mike",
}
]
const hasAmy = obj.some(x => x.groupName === 'Amy');
const hasMike = obj.some(x => x.groupName === 'mike');
console.log(hasAmy);
console.log(hasMike);
本文标签: javascriptHow to match key value in array of objectsStack Overflow
版权声明:本文标题:javascript - How to match key value in array of objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745018054a2637962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论