[英]Mongodb document with array of objects property: how match property of this array?
[英]How to match a property of an object to a property of an array of objects?
我有两个对象数组, hList
和sList
。 我试图给sList
的每个对象一个与hList.ID
匹配的hList.ID
(如果存在且尚未设置)。 我在逻辑上有点纠结,有人可以帮我吗?
var sList=[{name:"s1",id:"a"},{name: "s2",id:"b"},{name: "s3",id:"c"},{name: "s4",id:"d"}];
var hList=[{name: "h1",id:"x"},{name: "h2",id:"y"},{name: "h3",id:"z"}];
到目前为止,我有:
try{
for(i in sList)
{
var s=sList[i];
//find hList object that matches sList.id value
var h=_.filter(hList.id,function(i){return(i==s.hID)});
//if no hList.id match the sList.hID
//find hList object that doesnt match any
//set the source id to the hList object id
}
}catch(err){console.log("err: ",err);}
目标:(只有一个hList.id
可以匹配sList.hID
如果。 hList.id
犯规存在sList.hID
应该是不确定的。)
var sList=[{name:"s1",id:"a",hID:"x"},{name: "s2",id:"b",hID="y"},{name: "s3",id:"c",hID="z"},{name: "s4",id:"d",hID=undefined}]; //goal: set sList.hID to match an hList.id
var hList=[{name: "h1",id:"x"},{name: "h2",id:"y"},{name: "h3",id:"z"}];
var sList=[{name:"s1",id:"a"},{name: "s2",id:"b"},{name: "s3",id:"c"},{name: "s4",id:"d"}]; var hList=[{name: "h1",id:"x"},{name: "h2",id:"y"},{name: "h3",id:"z"}]; sList = sList.map((item, index) => { item.hId = hList[index] ? hList[index].id : undefined; return item }) console.log(sList);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.