Is it possible to look for arr
elements in arrofobjs
without a for loop? Since 'Buddy'
is in both arr
and arrofobjs
, i'd expect found
to return true
var arrofobjs = [ { type: 'Dog', name: 'Buddy', color: 'White' },
{ type: 'Cat', name: 'Misty', color: 'Black' },
{ type: 'Dog', name: 'Max', color: 'Black' }, ]
var arr = [ 'Buddy', 'Oscar' ]
var found = Object.values(arrofobjs).some(r=> arr.includes(r)) //returns false, but would return true if arrofobj was an object
Is it possible to look for arr
elements in arrofobjs
without a for loop? Since 'Buddy'
is in both arr
and arrofobjs
, i'd expect found
to return true
var arrofobjs = [ { type: 'Dog', name: 'Buddy', color: 'White' },
{ type: 'Cat', name: 'Misty', color: 'Black' },
{ type: 'Dog', name: 'Max', color: 'Black' }, ]
var arr = [ 'Buddy', 'Oscar' ]
var found = Object.values(arrofobjs).some(r=> arr.includes(r)) //returns false, but would return true if arrofobj was an object
Share
Improve this question
edited Jul 23, 2018 at 7:36
H Min
asked Jul 23, 2018 at 7:16
H MinH Min
818 bronze badges
2
-
3
I think you want
arr.includes(r.name)
<< you need to look for thename
attribute – Mark Commented Jul 23, 2018 at 7:19 -
2
I reckon that you should explicitly say "without a for loop", or maybe defining the kind of loop you want to avoid (
while
,forEach
etc), because all answers here (some
,includes
,find
etc...) do use loops internally. After all, if you stop to think, from the machine viewpoint this is impossible to do without looping. – Gerardo Furtado Commented Jul 23, 2018 at 7:27
3 Answers
Reset to default 5You have to access the name
property.
var arrofobjs = [ { type: 'Dog', name: 'Buddy', color: 'White' },
{ type: 'Cat', name: 'Misty', color: 'Black' },
{ type: 'Dog', name: 'Max', color: 'Black' }, ]
var arr = [ 'Buddy', 'Oscar' ]
var found = Object.values(arrofobjs).some(r => arr.includes(r.name))
console.log(found);
Since arrofobjs
is an array, you can directly apply the some
method by using destructing.
var arrofobjs = [ { type: 'Dog', name: 'Buddy', color: 'White' },
{ type: 'Cat', name: 'Misty', color: 'Black' },
{ type: 'Dog', name: 'Max', color: 'Black' }, ]
var arr = [ 'Buddy', 'Oscar' ]
var found = arrofobjs.some(({name}) => arr.includes(name))
console.log(found);
You were almost there. As arrofobjs
is an array, you can directly iterate over it.
var arrofobjs = [ { type: 'Dog', name: 'Buddy', color: 'White' },{ type: 'Cat', name: 'Misty', color: 'Black' },{ type: 'Dog', name: 'Max', color: 'Black' }];
var arr = [ 'Buddy', 'Oscar' ];
var found = arrofobjs.some(({name})=> arr.includes(name));
console.log(found);
You could also use Array.prototype.find()
method of array to find the record in array.
DEMO
var arrofobjs = [ { type: 'Dog', name: 'Buddy', color: 'White' },
{ type: 'Cat', name: 'Misty', color: 'Black' },
{ type: 'Dog', name: 'Max', color: 'Black' }],
arr = ['Buddy', 'Oscar'];
arr.forEach(v=>console.log(arrofobjs.find(({name})=>name==v)||`${v} Not fond`));
.as-console-wrapper {max-height: 100% !important;top: 0;}