[英]how to compare array of ids with object array nested property with underscore
I am working with a project that does not use jquery. 我正在使用一个不使用jquery的项目。 I have been using underscore or vanilla javascript.
我一直在使用下划线或香草javascript。 Here is a plunker I have with what I got.
这是我所拥有的一个笨蛋。 plunker
矮人
// Code goes here
var successfullIdArray = ["713","281","555"];
var tmpTripsArry = [{
"routeUUID": "123",
"routeLocations": {
"isSubmitLocationSuccess": false,
"routeUUID": "123",
"locationId": "713"
}
},{
"routeUUID": "909",
"routeLocations": {
"isSubmitLocationSuccess": false,
"routeUUID": "909",
"locationId": "281"
}
},{
"routeUUID": "800",
"routeLocations": {
"isSubmitLocationSuccess": false,
"routeUUID": "800",
"locationId": "555"
}
},{
"routeUUID": "444",
"routeLocations": {
"isSubmitLocationSuccess": false,
"routeUUID": "444",
"locationId": "200"
}
}];
var tmpUpdatedTripsArry = _.each(tmpTripsArry, function () {
_.find(tmpTripsArry, function (item) {
if (successfullIdArray.locationId === item.routeLocations.locationId) {
item.routeLocations.isSubmitLocationSuccess = true;
return item;
} else {
return item;
}
});
});
console.log(tmpUpdatedTripsArry)
I need to compare the tmpTripsArry nested property of locationId against the successfullIdArray and return the matching objects with the 我需要将locationId的tmpTripsArry嵌套属性与successlIdArray进行比较,然后将匹配的对象与
isSubmitLocationSuccess
isSubmitLocationSuccess
changed to true. 更改为true。
I am not getting any errors but it is not doing what I need it to do. 我没有收到任何错误,但它没有执行我需要做的事情。 thanks.
谢谢。 prefer to use underscore
喜欢使用下划线
simply js 只是js
var matchingObjs = tmpTripsArry.filter( function(val){
if ( successfullIdArray.indexOf( val.routeLocations.locationId ) != -1 )
{
val.routeLocations.isSubmitLocationSuccess = true;
}
return successfullIdArray.indexOf( val.routeLocations.locationId ) != -1 ;
})
If you need all the objects to be returned, then use forEach instead 如果需要返回所有对象,请使用forEach代替
tmpTripsArry.forEach( function(val){
if ( successfullIdArray.indexOf( val.routeLocations.locationId ) != -1 )
{
val.routeLocations.isSubmitLocationSuccess = true;
}
})
Now tmpTripsArry
itself has the modified values. 现在,
tmpTripsArry
本身具有修改后的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.