我定义了一个看起来像的数组
const [weekDays, setWeekDays] = useState({
a: [
{ id: 1, weekDay: 'MONDAY', isChecked: false },
{ id: 2, weekDay: 'TUESDAY', isChecked: false },
{ id: 3, weekDay: 'WEDNESDAY', isChecked: false },
{ id: 4, weekDay: 'THURSDAY', isChecked: false },
{ id: 5, weekDay: 'FRIDAY', isChecked: false },
{ id: 6, weekDay: 'SATURDAY', isChecked: false },
{ id: 7, weekDay: 'SUNDAY', isChecked: false },
]
});
另一个看起来像的阵列
const [array, setArray] = useState([{"id":2,"messageId":1,"weekDay":"Tuesday"}])
现在,我想比较数组,并将一个数组字段的isChecked值设置为true(如果另一个数组中存在工作日(。所以我想要一些类似的东西:
weekDays.a.map(weekDay => {
array.map(row => {
if (row.weekDay === weekDay.weekDay) {
setWeekDays([weekDay.isChecked = true])}
})
})
但我得到了错误无法读取未定义星期的属性地图.a.map
怎样才能正确地设置?
主要问题是比较两个不同的字符串。我已经使用CCD_ 1方法来转换字符串"的所有小写字符;星期二";为大写,以便可以应用该条件。
我使用索引号根据应用的条件将isChecked的值更改为true。
weekDays.a.map((weekDay, indexWeek) => {
array.map((row) => {
if (weekDay.weekDay == row.weekDay.toUpperCase()) {
console.log(weekDay, indexWeek);
weekDays.a[indexWeek].isChecked = true;
}
});
});