简体   繁体   English

如何从对象数组中检索 object 并使用 javascript 检索 arrays

[英]how to retrieve the object from array of objects and arrays using javascript

I have array of objects and arrays.我有对象数组和 arrays。

If the array of objects has value property same如果对象数组具有value property same

  • and if place includes of arrraylist list return first obj如果place包括 arrraylist list返回第一个 obj

  • and if place is equal includes/not includes return first obj如果place相等,则包含/不包含返回第一个 obj

if no above conditions return undefined;如果没有以上条件则返回undefined; using javascript使用 javascript

var list=['SG','TH','MY']


var arrobj1=[
  {id:1, name:'userone',place:'SG', value:100},
  {id:2, name:'usertwo',place:'TH', value:100},
  {id:3, name:'userthree',place:'IL',value:200},
]
Expected Output
{id:1, name:'userone',place:'SG', value:100}

****
var arrobj2=[
  {id:1, name:'userone',place:'IN', value: 200},
  {id:2, name:'usertwo',place:'SL',value: 100},
  {id:3, name:'userthree',place:'SL', value: 100},
]
Expected Output
{id:2, name:'usertwo',place:'SL',value: 100}
****
var arrobj3=[
  {id:1, name:'userone',place:'SL', value:10},
  {id:2, name:'usertwo',place:'IN', value:20},
  {id:3, name:'userthree',place:'KL', value:30},
]
Expected Output
undefined

Tried尝试过

var result= arrobj.find(e=>{
  if((e.value === e.value) && (list.includes(e.place)){
   return e
  }
})


I've added inline comments to correspond to the requirements.我已经添加了内联注释以符合要求。 I wasn't sure exactly what you meant in some cases, so I can adjust the answer if you clarify the question在某些情况下我不确定你的意思,所以如果你澄清问题我可以调整答案

 var list = ['SG', 'TH', 'MY'] var arrobj1 = [{ id: 1, name: 'userone', place: 'SG', value: 100 }, { id: 2, name: 'usertwo', place: 'TH', value: 100 }, { id: 3, name: 'userthree', place: 'IL', value: 200 }, ] //Expected Output //{id:1, name:'userone',place:'SG', value:100} //**** var arrobj2 = [{ id: 1, name: 'userone', place: 'IN', value: 200 }, { id: 2, name: 'usertwo', place: 'SL', value: 100 }, { id: 3, name: 'userthree', place: 'SL', value: 100 }, ] //Expected Output //{id:2, name:'usertwo',place:'SL',value: 100} //**** var arrobj3 = [{ id: 1, name: 'userone', place: 'SL', value: 10 }, { id: 2, name: 'usertwo', place: 'IN', value: 20 }, { id: 3, name: 'userthree', place: 'KL', value: 30 }, ] //Expected Output //undefined function getMatch(places, arrObj) { //.find will return the first item that matches the condition provided return arrObj.find( l => list.includes(l.place) // either the place of this item must exist in the list || arrObj.filter(arr => arr.value === l.value).length > 1) // or the value of this item must appear more than once in the list } console.log(getMatch(list, arrobj1)); console.log(getMatch(list, arrobj2)); console.log(getMatch(list, arrobj3));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从对象数组中检索随机对象? 使用Javascript - How do I retrieve a random object from an array of objects ? Javascript Javascript:使用对象而不是数组按ID检索对象 - Javascript: retrieve objects by id, using an object, not an array 如何使用 javascript 从对象数组中检索选择性属性并存储为 object? - How to retrieve selective properties from the array of objects and store as an object using javascript? 如何通过 88222995402888 和 javascript 中的 arrays 获取对象数组 - How to get array of objects by object and arrays in javascript 如何使用JavaScript从Firebase实时数据库中检索对象数组? - How to retrieve an array of objects from a firebase realtime database using javascript? 如何使用嵌套数组在javascript中使用第一个数组作为对象并将数组的其余部分作为值来形成对象 - how to use nested arrays to form objects in javascript using the first array as object and the rest of the array as value 将带有对象数组的javascript对象格式化为Array of Array - To format javascript Object with Array of Objects to Array Of Arrays 如何从javascript对象制作数组数组? - How to make an array of arrays from a javascript object? 如何从包含字符串、对象和 arrays 的 Javascript object 中检索动态指定、任意和深度嵌套的值? - How can I retrieve dynamically specified, arbitrary and deeply nested values from a Javascript object containing strings, objects, and arrays? 如何使用javascript将object of object个数组对象改成数组对象 - How to change object of object of array objects to array objects using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM