最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript find Array elements within Array of Objects values without a for loop - Stack Overflow

programmeradmin3浏览0评论

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 the name 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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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;}

发布评论

评论列表(0)

  1. 暂无评论