如何使用for循环在另一个对象中设置属性值?

问题描述 投票:0回答:1

我在设置对象属性值时遇到了一个大问题(对我来说,这很大)。我有一个这样的对象:

const newPlan = {
    name: '',
    description: '',
    number: '',
    monday: {
        breakfast: '',
        secondBreakfast: '',
        soup: '',
        secondMeal: '',
        supper: '',
    }
};

键的值取决于用户输入。设置前三个值很容易,但是我在对象值中遇到对象问题,因此必须使用此循环。

有类似HTML的代码:

<tr>
  <td>PONIEDZIAŁEK</td>
  <td>
    <input list="monday-breakfast" id="monday-1">
    <datalist id="monday-breakfast">
    </datalist>
  </td>
  <td>
    <input list="monday-second-breakfast" id="monday-2">
    <datalist id="monday-second-breakfast">
    </datalist>
  </td>
  <td>
    <input list="monday-soup" id="monday-3">
    <datalist id="monday-soup">
    </datalist>
  </td>
  <td>
    <input list="monday-second-meal" id="monday-4">
    <datalist id="monday-second-meal">

    </datalist>
  </td>
  <td><input list="monday-supper" id="monday-5">
    <datalist id="monday-supper">
    </datalist></td>
</tr>

和我的js:

newPlan.name = planName.value;
newPlan.description = planDescription.value;
newPlan.number = planWeekNumber.value;
for (let i = 0; i < Object.keys(newPlan.monday).length; i++) {
    for (let j = 0; j < allDays.length; j++) {
        Object.values(newPlan.monday)[i] = 'abc';
        break
    }
}

'abc'值仅是示例-我想基于输入列表值设置值,该列表基于用户的选择。因此有<input list="monday-breakfast">等。并且此输入中的每个值都应为正确的键值。最后,它应该像这样:

const newPlan = {
    name: 'some string from input',
    description: 'some string from input',
    number: 'some string from input',
    monday: {
        breakfast: 'some string from input list',
        secondBreakfast: 'some string from input list',
        soup: 'some string from input list',
        secondMeal: 'some string from input list',
        supper: 'some string from input list',
    }
};
javascript html object for-loop dom
1个回答
0
投票

您需要用monday循环index项,并将当前key的值设置为monday-index元素的值。

这里是一个例子:

const newPlan = {
  name: '',
  description: '',
  number: '',
  monday: {
    breakfast: '',
    secondBreakfast: '',
    soup: '',
    secondMeal: '',
    supper: '',
  }
};

let index = 1;

newPlan.name = document.getElementById('planName').innerHTML;
newPlan.description = document.getElementById('planDescription').innerHTML;
newPlan.number = document.getElementById('planWeekNumber').innerHTML;

for (let [key, value] of Object.entries(newPlan.monday)) {
  newPlan.monday[key] = document.getElementById(`monday-${index}`).value;
  index++;
}

console.log(newPlan);
<tr>
  <td>PONIEDZIAŁEK</td>
  <div id="planName">Plan 1</div>
  <div id="planDescription">Tasty food.</div>
  <div id="planWeekNumber">1</div>
  <td>
    <input list="monday-breakfast" id="monday-1" value="Ham and eggs">
    <datalist id="monday-breakfast"></datalist>
  </td>
  <td>
    <input list="monday-second-breakfast" id="monday-2" value="Cereal">
    <datalist id="monday-second-breakfast"></datalist>
  </td>
  <td>
    <input list="monday-soup" id="monday-3" value="Meat soup">
    <datalist id="monday-soup"></datalist>
  </td>
  <td>
    <input list="monday-second-meal" id="monday-4" value="Hamburger">
    <datalist id="monday-second-meal"></datalist>
  </td>
  <td>
    <input list="monday-supper" id="monday-5" value="Toast">
    <datalist id="monday-supper"></datalist>
  </td>
</tr>
© www.soinside.com 2019 - 2024. All rights reserved.