undefinedfix
Sign in

Two JS arrays match replacement values

jnewman edited in Tue, 31 May 2022

How can I match the values in two arrays

var obj1 = [
  {name: '颜色', checked: true, must: true},
  {name: '码数', checked: true, must: true},
  {name: '码数2'}
  {name: '码数3'}
  {name: '码数4'}
]

var obj2 = ['颜色','码数','码数2','码数4']

The value matching obji2 should correspond to the value of name in obji1. If checked is true, it will be set to false, otherwise it will be the opposite. But if there is a must in the object that is true, is there any way to keep checked unchanged without double loop?

The final results are as follows

[
  {name: '颜色', checked: true, must: true},
  {name: '码数', checked: true, must: true},
  {name: '码数2', checked: true}
  {name: '码数3'}
  {name: '码数4', checked: true}
]
7 Replies
Toktam
commented on Tue, 31 May 2022
for(var i=0;i<obj1.length;i++){
 if(obj2.indexOf(obj1[i].name)!= -1)
   obj1[i].checked = !obj1[i].must?!obj1[i].checked:true;
}
xtu
commented on Wed, 01 Jun 2022

Do you want to be faster or simpler? If it's more concise, look at the answer of afishhhh. If it's faster, you need to reset obji2 to hash mode

let hash={}
obj2.forEach(name=>hash[name]=true)
obj1.forEach(item=>{
  if(hash[item.name]){
    item.checked=item.must || !item.checked
  }
})
FactorVIII
commented on Wed, 01 Jun 2022
obj2.forEach(e => {
  const _ = obj1.find(_e => _e.name == e)
  _.checked = _.must || !_.checked
})

So?

user617398
commented on Wed, 01 Jun 2022
obj1.forEach(item1 => {
  if(obj2.includes(item1.name)){
    item1 = action(item1)
  }
})

function action(obj){
  if(!obj.must === true){
    obj.checked ?
      obj.checked = !obj.checked :
      obj.checked = true
  }
}
k0r3n
commented on Wed, 01 Jun 2022
var obj1 = [
    { name: '颜色', checked: true, must: true },
    { name: '码数', checked: true, must: true },
    { name: '码数2' },
    { name: '码数3' },
    { name: '码数4' }
]

var obj2 = ['颜色', '码数', '码数2', '码数4']

const newObj1 = obj1.map(item => {
    if (obj2.includes(item.name)) {
        const checked = item.must ? item.checked : !item.checked
        return { ...item, checked }
    } else {
        return item
    }
})
console.log(newObj1)
BingLi224
commented on Wed, 01 Jun 2022

Double loop doesn't quite understand what it means. In fact, it is to traverse ob911 to determine whether obji2 exists for each name. Right. check(obj1 Item.name Suppose there is such a way. You call this method to judge. Don't worry about the internal implementation. Simple and easy to understand are the methods of indexof, FindIndex, find and include. Of course, the quickest way is to change obji2 into an object form, with name as the key. With such a hash structure, you can traverse it, and subsequent queries will only visit the object. Don't look up the array

I recommend hash, which is stonehook's answer

elinx
commented on Wed, 01 Jun 2022
 function isBelong(name, arr) {
       for(let i = 0; i < arr.length; i++){
          if(name === arr[i]){
           return true;
          }
       }
       return false;
     }

       const newobj = obj1.map(item => {
         if(isBelong(item.name, obj2) && !item.must){
            return {
              ...item,
              checked: !item.checked
            }
         }
         return item;
       });
lock This question has been locked and the reply function has been disabled.