如何使用JavaScript从一系列对象和数组中检索对象

发布于 2025-01-23 18:27:58 字数 1101 浏览 0 评论 0原文

我有各种各样的对象和数组。

如果对象的数组具有value属性相同

  • ,并且如果plot包括arrraylist list> list返回obj

  • ,并且如果place等于/不包括/不包括返回obj

如果上述条件返回未定义;使用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

尝试

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


I have array of objects and arrays.

If the array of objects has value property same

  • and if place includes of arrraylist list return first obj

  • and if place is equal includes/not includes return first obj

if no above conditions return undefined; using 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
  }
})


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2025-01-30 18:27:58

我添加了内联注释,以对应于要求。在某些情况下,我不确定您的意思,所以如果您澄清问题,我可以调整答案

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));

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));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文