gpt4 book ai didi

javascript - 如何从具有值字符串的数组更改对象数组中的值

转载 作者:行者123 更新时间:2023-11-28 17:11:00 25 4
gpt4 key购买 nike

我有两个数组,基于 daysActive 值中的数组,我想使用 value 键值解析第二个对象数组并更改 checked 键值为 true

var daysActive = ['monday', 'tuesday', 'wednesday'];

var weekDays = [{ "name": "Sunday", "value": "sunday", "id": "SUN","checked":false,disabled:true },
{ "name": "Monday", "value": "monday", "id": "MON" ,"checked":false,disabled:true},
{ "name": "Tuesday", "value": "tuesday", "id": "TUE","checked":false,disabled:true },
{ "name": "Wednesday", "value": "wednesday", "id": "WED","checked":false,disabled:true },
{ "name": "Thursday", "value": "thursday", "id": "THU","checked":false,disabled:true },
{ "name": "Friday", "value": "friday", "id": "FRI","checked":false,disabled:true },
{ "name": "Saturday", "value": "saturday", "id": "SAT","checked":false,disabled:true}
];

结果数组

var resultantDays = [{ "name": "Sunday", "value": "sunday", "id": "SUN","checked":false, disabled:true },
{ "name": "Monday", "value": "monday", "id": "MON" ,"checked":true, disabled:true},
{ "name": "Tuesday", "value": "tuesday", "id": "TUE","checked":true, disabled:true },
{ "name": "Wednesday", "value": "wednesday", "id": "WED","checked":true, disabled:true },
{ "name": "Thursday", "value": "thursday", "id": "THU","checked":false, disabled:true },
{ "name": "Friday", "value": "friday", "id": "FRI","checked":false, disabled:true },
{ "name": "Saturday", "value": "saturday", "id": "SAT","checked":false, disabled:true}
];

我已经尝试过,但它的方法不正确。

var modifiedWeekdays = days.map(value => {
weekdays.forEach(function(val, key) {
Object.entries(val).forEach(([keyObj, valObj]) => {
if (keyObj === 'value' && valObj === value) {

}
});
});
});

最佳答案

您可以在 weekDays 数组中使用 map 函数来查看该值是否在 daysActive 数组中

var daysActive = ['monday', 'tuesday', 'wednesday'];

var weekDays = [{ "name": "Sunday", "value": "sunday", "id": "SUN","checked":false,disabled:true },
{ "name": "Monday", "value": "monday", "id": "MON" ,"checked":false,disabled:true},
{ "name": "Tuesday", "value": "tuesday", "id": "TUE","checked":false,disabled:true },
{ "name": "Wednesday", "value": "wednesday", "id": "WED","checked":false,disabled:true },
{ "name": "Thursday", "value": "thursday", "id": "THU","checked":false,disabled:true },
{ "name": "Friday", "value": "friday", "id": "FRI","checked":false,disabled:true },
{ "name": "Saturday", "value": "saturday", "id": "SAT","checked":false,disabled:true}
];
weekDays.map(day => {
day.checked = daysActive.indexOf(day.value) !== -1;
});
console.log(weekDays);

关于javascript - 如何从具有值字符串的数组更改对象数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54434252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com