根据字符串数组更新对象数组

huangapple go评论60阅读模式
英文:

Update Array of Objects based on Array of strings

问题

我正在尝试更新一个对象数组。基于用户的选择或字符串数组,将active键设置为true。

例如,选择ABC, DEF后,这两个ID的active键将设置为true

  {
    "active": true,
    "id": 1,
    "name": "ABC",
    "value": "1"
  },
  {
    "active": true,
    "id": 2,
    "name": "DEF",
    "value": "2"
  },
  {
    "active": true,
    "id": 3,
    "name": "GHI",
    "value": "3"
  }
]
  "ABC",
  "DEF"
]

现在我需要基于默认数组创建一个新数组,并且只为selectedArray中的字符串数组分配active=true

结果看起来像这样:

  {
    "active": true,
    "id": 1,
    "name": "ABC",
    "value": "1"
  },
  {
    "active": true,
    "id": 2,
    "name": "DEF",
    "value": "2"
  },
  {
    "active": false,
    "id": 3,
    "name": "GHI",
    "value": "3"
  }
]

我尝试了以下方法。

arrCopy.forEach(x => x.active = false)
arrCopy.forEach(x => {
    if(selectedArray.includes(x.name))
        x.active =  true
})

console.log(arrCopy)

上述代码有效,但是否有更标准的方法来实现相同的目标?谢谢!

英文:

I am trying to update an array of objects. The active key will be set to true based on user's selection or the array of strings.

For an instance, on selecting ABC, DEF, the active key will be set to true for these two Ids.

const defaultArray = [
  {
    "active": true,
    "id": 1,
    "name": "ABC",
    "value": "1"
  },
  {
    "active": true,
    "id": 2,
    "name": "DEF",
    "value": "2"
  },
  {
    "active": true,
    "id": 3,
    "name": "GHI",
    "value": "3"
  }
]
const selectedArray = [
  "ABC",
  "DEF"
]

So now I need to create a new Array based on the default array and assign active=true only for array of string in selecetedArray.

Result will look like

const newArray = [
  {
    "active": true,
    "id": 1,
    "name": "ABC",
    "value": "1"
  },
  {
    "active": true,
    "id": 2,
    "name": "DEF",
    "value": "2"
  },
  {
    "active": false,
    "id": 3,
    "name": "GHI",
    "value": "3"
  }
]

I tried.

const arrCopy = [...defaultArray]
arrCopy.forEach(x => x.active = false)
arrCopy.forEach(x => {
    if(selectedArray.includes(x.name))
        x.active =  true
})

console.log(arrCopy)

The above code works, but is there any more standard way to achieve the same. thanks

答案1

得分: 5

你不需要两个循环来实现这个,你可以直接在defaultArray上使用map方法。

defaultArray.map(x => ({...x, active: selectedArray.includes(x.name)}))
const defaultArray = [
  {
    "active": true,
    "id": 1,
    "name": "ABC",
    "value": "1"
  },
  {
    "active": true,
    "id": 2,
    "name": "DEF",
    "value": "2"
  },
  {
    "active": true,
    "id": 3,
    "name": "GHI",
    "value": "3"
  }
]
const selectedArray = [
  "ABC",
  "DEF"
]

const newArray = defaultArray.map(x => ({...x, active: selectedArray.includes(x.name)}))

console.log(newArray)
英文:

You don't need two loops to achieve that, you can directly use map method on the defaultArray.

defaultArray.map(x => ({...x, active: selectedArray.includes(x.name)}))

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const defaultArray = [
  {
    &quot;active&quot;: true,
    &quot;id&quot;: 1,
    &quot;name&quot;: &quot;ABC&quot;,
    &quot;value&quot;: &quot;1&quot;
  },
  {
    &quot;active&quot;: true,
    &quot;id&quot;: 2,
    &quot;name&quot;: &quot;DEF&quot;,
    &quot;value&quot;: &quot;2&quot;
  },
  {
    &quot;active&quot;: true,
    &quot;id&quot;: 3,
    &quot;name&quot;: &quot;GHI&quot;,
    &quot;value&quot;: &quot;3&quot;
  }
]
const selectedArray = [
  &quot;ABC&quot;,
  &quot;DEF&quot;
]

const newArray = defaultArray.map(x =&gt; ({...x, active: selectedArray.includes(x.name)}))

console.log(newArray)

<!-- end snippet -->

答案2

得分: 1

你可以简单地遍历你的对象数组,检查selectedArray是否包含每个对象的指定name属性,并将结果设置为active标志,如下所示 -

const defaultArray = [
  {
    "active": true,
    "id": 1,
    "name": "ABC",
    "value": "1"
  },
  {
    "active": true,
    "id": 2,
    "name": "DEF",
    "value": "2"
  },
  {
    "active": true,
    "id": 3,
    "name": "GHI",
    "value": "3"
  }
];

const selectedArray = [
  "ABC",
  "DEF"
];

const arrCopy = [...defaultArray];
arrCopy.forEach(x => x.active = selectedArray.includes(x.name));

console.log(arrCopy);
英文:

You can simply iterate through your array of objects & see if selectedArray includes the specified name attribute from each object and set the result to active flag, like the following -

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const defaultArray = [
  {
    &quot;active&quot;: true,
    &quot;id&quot;: 1,
    &quot;name&quot;: &quot;ABC&quot;,
    &quot;value&quot;: &quot;1&quot;
  },
  {
    &quot;active&quot;: true,
    &quot;id&quot;: 2,
    &quot;name&quot;: &quot;DEF&quot;,
    &quot;value&quot;: &quot;2&quot;
  },
  {
    &quot;active&quot;: true,
    &quot;id&quot;: 3,
    &quot;name&quot;: &quot;GHI&quot;,
    &quot;value&quot;: &quot;3&quot;
  }
];

const selectedArray = [
  &quot;ABC&quot;,
  &quot;DEF&quot;
];

const arrCopy = [...defaultArray];
arrCopy.forEach(x =&gt; x.active =  selectedArray.includes(x.name));

console.log(arrCopy);

<!-- end snippet -->

答案3

得分: 0

首先,您可以删除这行代码:

arrCopy.forEach(x => x.active = false)

然后改成这样:

arrCopy.forEach(x => {
    if (selectedArray.includes(x.name)) {
        x.active = true;
    } else {
        x.active = false;
    }
})

如果您的代码中会重复出现这段逻辑,您可以编写一个函数来实现这个目标。

英文:

first you can remove this line :

arrCopy.forEach(x =&gt; x.active = false)

and do it like this :

arrCopy.forEach(x =&gt; {
    if(selectedArray.includes(x.name)){
        x.active =  true}
    else{
       x.active =  false
     }    
})

you can write a function to do achieve that goal if this piece would be repeated in your code

答案4

得分: 0

将新参数添加到checkActive函数中将检查对象中是否存在名称。

该函数如下:

function checkActive(...args){
    return  newArray.map(obj => {
         if(args.includes(obj.name)){
              obj.active = true
         }else{
              obj.active = false
         }
        return obj
      })

}
console.log(checkActive("ABC","DEF"))
英文:

Adding new arguments to the checkActivefunction will check in the object if the name exists.

The function is:

function checkActive(...args){
    return  newArray.map(obj=&gt;{
         if(args.includes(obj.name)){
              obj.active = true
         }else{
              obj.active = false
         }
        return obj
      })

}
console.log(checkActive(&quot;ABC&quot;,&quot;DEF&quot;))

huangapple
  • 本文由 发表于 2023年3月7日 14:15:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定