如何使用JavaScript从一系列对象和数组中检索对象
我有各种各样的对象和数组。
如果对象的数组具有value属性相同
,并且如果
plot
包括arrraylistlist> 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 arrraylistlist
return first objand 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我添加了内联注释,以对应于要求。在某些情况下,我不确定您的意思,所以如果您澄清问题,我可以调整答案
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