Vue corresponds the values in the two arrays.

clipboard.png

clipboard.png
if the id of two arrays is equal, I will change the activeName field in the first array to true,. I use this to determine whether he is added to the collection or not. If true is added, false is not added. I added the fields that the background did not return to me with such a status. So the list array that needs to be collected should be compared with my current two. Id, if id is equal, it means that one record is in a collection state, that is, it is rewritten to true.
. How do these two arrays determine that when major_id equals id, make the activeName field true:

?
    _this.listWish.forEach(function(a) {
      _this.major.forEach(function(item) {
        if (item.major_id == a.id) {
          _this.$set(a, "activeName", true);
          _this.$set(a, "wishId", item.wish_id);
        } else {
          _this.$set(a, "activeName", false);
        }
      });
    });

-sharp-sharp
but after I loop like this, the value of the activeName field is not rewritten correctly, it"s all false.

Apr.03,2021

your cycle is not right, according to your meaning

make the activeName field true when judging that major_id is equal to id

if you loop like this, if the penultimate number matches and sets to true, and the penultimate one doesn't match, isn't it false again?
you can change it to for loop

for(let i = 0; i < _this.listWish.length; i PP){
    for(let j = 0; i < _this.major.length; j PP){
        if (item.major_id == a.id) {
            _this.$set(a, 'activeName', true);
            _this.$set(a, 'wishId', item.wish_id);
            break;
        } else {
            _this.$set(a, 'activeName', false);
        }
    }
}

the field returned to you by the background does not have a favorite field?


//
var _tag = false;
_this.listWish.forEach(function(a) {
  _tag = false;
  _this.major.forEach(function(item) {
    if (item.major_id == a.id) {
      _tag  = item.wish_id
    }
  });
  if(_tag){
      _this.$set(a, 'activeName', true);
      _this.$set(a, 'wishId', _tag);
  } else {
      _this.$set(a, 'activeName', false);
  }
});
//

of course, I suggest changing the contents to Object and major_id as key , so that you can directly look up, major_id_ hash [item.major _ id] , assign a value if you can get it, and false if you can't.

Menu