如果匹配更改为isreccommende:true,则通过比较id来检查对象数组的匹配

时间:2018-08-14 07:55:47

标签: javascript

final Context context = RuntimeEnvironment.application;

1 个答案:

答案 0 :(得分:0)

您需要遍历array2,并使用array1检查Array.find()中是否存在对象。然后,您可以将找到的对象的isRecommended更改为true

var array1 = [{
  "_id": "5b7229fd5fd0b77763cd5d0b",
  "title": "Nexus of knowledge",
  "isRecommended": false,
}, {
  "_id": "5b7229d15fd0b77763cd5adc",
  "title": "Nexus of knowledge",
  "isRecommended": false,
}, {
  "_id": "5b7229c45fd0b77763cd5a11",
  "title": "Nexus of knowledge",
  "isRecommended": false,
}, {
  "_id": "5b7229be5fd0b77763cd59b9",
  "title": "Nexus of knowledge",
  "isRecommended": false,
}, {
  "_id": "5b7229be5fd0b77763cd59bf",
  "title": "Nexus of knowledge",
  "isRecommended": false,
}, {
  "_id": "5b7229c15fd0b77763cd59fb",
  "title": "Nexus of knowledge",
  "isRecommended": false,
}];
var array2 = [{
  "_id": "5b7229c15fd0b77763cd59fb"
}, {
  "_id": "5b7229be5fd0b77763cd59bf"
}, {
  "_id": "5b7229be5fd0b77763cd59b9"
}];

array2.forEach((item) => {
  var array1Obj = array1.find(({_id}) => item._id === _id);
  if(array1Obj) {
    array1Obj.isRecommended = true;
  }
});
console.log(array1);