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

时间:2019-01-30 06:07:15

标签: javascript arrays ecmascript-6

我有两个数组,基于值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) {

       }
    });
  });
});

4 个答案:

答案 0 :(得分:3)

您可以在map数组中使用weekDays函数,以查看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);

答案 1 :(得分:0)

类似于@Vineesh解决方案:)

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
  }
];

daysActive.forEach(dayActive => {
  const activeDay = weekDays.find(day => day.value === dayActive);
  activeDay.checked = !activeDay.checked;
});

console.log(weekDays);

答案 2 :(得分:0)

您可以将Array.prototype.reduce()Array.prototype.includes()组合使用

代码:

const daysActive = ['monday', 'tuesday', 'wednesday'];
const 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}
];
const resultantDays = weekDays.reduce((a, c) => [...a, c.checked = daysActive.includes(c.value), c], []);

console.log(resultantDays);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

for (let dayOfWeek of daysActive){
    weekDays.filter(it => it['value']=== dayOfWeek)[0].checked=true;
}