I have an object that looks like this:
{
"examples":[
{
"key":"value1"
},
{
"key":"value1",
"key2":"example"
},
{
"key":"value1"
},
{
"key":"value2"
},
{
"key":"value2",
"key2":"example"
},
{
"key":"value2"
}
]
}
I'm trying to use findIndex to find where in the object that key === 'value1'
and key2
exists (so in this instance the index would be 1). I've tried using something like var x = examples.findIndex(({key}) => key === 'value1' && ({key2}) => key2)
but it's not working. How do I go about this? Any answers would be greatly appreciated! :)
examples
is a property of the data object, so you need to iterate over that array. examples.findIndex
won't do anything.
You can then check to see if key
has "value1" as a value, and that there is a key2
in the object.
const data={examples:[{key:"value1"},{key:"value1",key2:"example"},{key:"value1"},{key:"value2"},{key:"value2",key2:"example"},{key:"value2"}]}; const result = data.examples.findIndex(obj => { return obj.key === 'value1' && obj.key2; }); console.log(result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.