简体   繁体   English

通过数组迭代设置对象键值对

[英]Setting up object key-value pairs via array iteration

I am trying to create the following array by using for loop.我正在尝试使用 for 循环创建以下数组。 However, my result is an array with the length of 24 and all objects inside become { key: '23', text: '12:30', value: '12:30' } , rather than iterating one by one.然而,我的结果是一个长度为 24 的数组,里面的所有对象都变成了{ key: '23', text: '12:30', value: '12:30' } ,而不是一一迭代。 Can anyone explain to me why each iteration would override the previous one?谁能向我解释为什么每次迭代都会覆盖前一次?

const timeOptions = [
  { key: '0', text: '1:00', value: '1:00' },
  { key: '1', text: '1:30', value: '1:30' },
  { key: '2', text: '2:00', value: '2:00' },
  { key: '3', text: '2:30', value: '2:30' },
  { key: '4', text: '3:00', value: '3:00' },
  { key: '5', text: '3:30', value: '3:30' },
  { key: '6', text: '4:00', value: '4:00' },
  { key: '7', text: '4:30', value: '4:30' },
  { key: '8', text: '5:00', value: '5:00' },
  { key: '9', text: '5:30', value: '5:30' },
  { key: '10', text: '6:00', value: '6:00' },
  { key: '11', text: '6:30', value: '6:30' },
  { key: '12', text: '7:00', value: '7:00' },
  { key: '13', text: '7:30', value: '7:30' },
  { key: '14', text: '8:00', value: '8:00' },
  { key: '15', text: '8:30', value: '8:30' },
  { key: '16', text: '9:00', value: '9:00' },
  { key: '17', text: '9:30', value: '9:30' },
  { key: '18', text: '10:00', value: '10:00' },
  { key: '19', text: '10:30', value: '10:30' },
  { key: '20', text: '11:00', value: '11:00' },
  { key: '21', text: '11:30', value: '11:30' },
  { key: '22', text: '12:00', value: '12:00' },
  { key: '23', text: '12:30', value: '12:30' },
];

This is the function I wrote:这是我写的函数:

var array = Array(24).fill({});
array[0] = { key: '0', text: '1:00', value: '1:00' };

function transform(arr) {
  for (var i = 0; i < arr.length; i++) {
    arr[i].key = i.toString();
    if (i % 2 === 0 && i !== 0) {
      arr[i].text = Number(arr[i - 2].text.split(':')[0]) + 1 + ':00';
      arr[i].value = arr[i].text;
    } else if (i % 2 !== 0) {
      arr[i].text = arr[i - 1].text.split(':')[0] + ':30';
      arr[i].value = arr[i].text;
    }
  }
  return arr;
}

transform(array);

Your issue is with the very first line:您的问题出在第一行:

var array = Array(24).fill({});

Essentially you're saying create an array with a bunch of the same empty objects.基本上你是说用一堆相同的空对象创建一个数组。 So, when you change one, it will change all the others.所以,当你改变一个时,它会改变所有其他的。 You can think of this as each index in your array is pointing to the same memory location which is storing that empty object {} .您可以将其视为数组中的每个索引都指向存储该空对象{}的相同内存位置。

A quick fix to this is to manually fill your array using a for loop.对此的快速解决方法是使用 for 循环手动填充数组。 Take a look at the code snippet for a working example:查看一个工作示例的代码片段:

 var array = []; for(var i = 0; i < 24; i++) { array.push({}); } array[0] = { key: '0', text: '1:00', value: '1:00' }; function transform(arr) { for (var i = 0; i < arr.length; i++) { arr[i].key = i.toString(); if (i % 2 === 0 && i !== 0) { arr[i].text = Number(arr[i - 2].text.split(':')[0]) + 1 + ':00'; arr[i].value = arr[i].text; } else if (i % 2 !== 0) { arr[i].text = arr[i - 1].text.split(':')[0] + ':30'; arr[i].value = arr[i].text; } } return arr; } console.log(transform(array));

You do not need an additional for loop to fill the array.您不需要额外的 for 循环来填充数组。 Instead, just add arr[i] = {};相反,只需添加 arr[i] = {}; at the beginning of each iteration:在每次迭代开始时:

var array = Array(24);
array[0] = { key: '0', text: '1:00', value: '1:00' };

function transform(arr) {
  for (var i = 0; i < arr.length; i++) {
    **arr[i] = {};**
    arr[i].key = i.toString();
    if (i % 2 === 0 && i !== 0) {
      arr[i].text = Number(arr[i - 2].text.split(':')[0]) + 1 + ':00';
      arr[i].value = arr[i].text;
    } else if (i % 2 !== 0) {
      arr[i].text = arr[i - 1].text.split(':')[0] + ':30';
      arr[i].value = arr[i].text;
    }
  }
  return arr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM