【问题标题】:How I can assign a custom format to an array?如何为数组分配自定义格式?
【发布时间】:2021-12-12 07:07:14
【问题描述】:

我有一组用于映射的日期。当尝试使用索引从不同的数组(名称)传递项目时,我得到未定义的值,因为“名称”数组的条目少于索引(日期)。

我有第三个数组,应该是正确的格式(格式):

let names = [
  "chair",
  "table",
  "door",
  "window",
  "glass",
  "wine",
  "car",
  "keys",
  "dream",
  "keyboard",
  "vodka",
  "pepsi",
  "bag",
  "ikea",
  "mercedes",
  "soprano"
];
let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
let dates = [
  "2021-10-04T04:00:00.000Z",
  "2021-10-05T04:00:00.000Z",
  "2021-10-06T04:00:00.000Z",
  "2021-10-13T04:00:00.000Z",
  "2021-10-14T04:00:00.000Z",
  "2021-10-15T04:00:00.000Z",
  "2021-10-15T04:00:00.000Z",
  "2021-10-17T22:00:00.000Z",
  "2021-10-18T22:00:00.000Z",
  "2021-10-19T22:00:00.000Z",
  "2021-10-20T22:00:00.000Z",
  "2021-10-21T22:00:00.000Z",
  "2021-10-17T22:00:00.000Z",
  "2021-10-18T22:00:00.000Z",
  "2021-10-19T22:00:00.000Z",
  "2021-10-20T22:00:00.000Z",
  "2021-10-19T04:00:00.000Z",
  "2021-10-20T04:00:00.000Z",
  "2021-10-21T04:00:00.000Z",
  "2021-10-22T04:00:00.000Z",
  "2021-10-19T04:00:00.000Z",
  "2021-10-20T04:00:00.000Z",
  "2021-10-21T04:00:00.000Z",
  "2021-10-25T04:00:00.000Z",
  "2021-10-26T04:00:00.000Z",
  "2021-10-27T04:00:00.000Z",
  "2021-10-28T04:00:00.000Z",
  "2021-10-29T04:00:00.000Z",
  "2021-10-25T04:00:00.000Z",
  "2021-10-26T04:00:00.000Z",
  "2021-10-27T04:00:00.000Z",
  "2021-10-28T04:00:00.000Z",
  "2021-10-29T04:00:00.000Z",
  "2021-11-01T04:00:00.000Z",
  "2021-11-02T04:00:00.000Z",
  "2021-11-03T04:00:00.000Z",
  "2021-11-04T04:00:00.000Z",
  "2021-11-05T04:00:00.000Z",
  "2021-11-08T04:00:00.000Z",
  "2021-11-09T04:00:00.000Z",
  "2021-11-10T04:00:00.000Z",
  "2021-11-01T04:00:00.000Z",
  "2021-11-02T04:00:00.000Z",
  "2021-11-03T04:00:00.000Z",
  "2021-11-04T04:00:00.000Z",
  "2021-11-05T04:00:00.000Z",
  "2021-11-08T04:00:00.000Z",
  "2021-11-09T04:00:00.000Z",
  "2021-11-10T04:00:00.000Z",
  "2021-11-11T04:00:00.000Z",
  "2021-11-12T04:00:00.000Z",
  "2021-11-11T04:00:00.000Z",
  "2021-11-12T04:00:00.000Z",
  "2021-11-13T04:00:00.000Z",
  "2021-11-15T04:00:00.000Z",
  "2021-11-16T04:00:00.000Z",
  "2021-11-17T04:00:00.000Z",
  "2021-11-18T04:00:00.000Z",
  "2021-11-19T04:00:00.000Z",
  "2021-11-16T04:00:00.000Z",
  "2021-11-17T04:00:00.000Z",
  "2021-11-18T04:00:00.000Z",
  "2021-11-19T04:00:00.000Z",
  "2021-11-20T04:00:00.000Z",
  "2021-11-23T04:00:00.000Z",
  "2021-11-24T04:00:00.000Z",
  "2021-11-23T04:00:00.000Z",
  "2021-11-24T04:00:00.000Z",
  "2022-01-05T04:00:00.000Z",
  "2022-01-06T04:00:00.000Z",
  "2022-01-07T04:00:00.000Z",
  "2022-01-10T04:00:00.000Z",
  "2022-01-11T04:00:00.000Z",
  "2022-01-12T04:00:00.000Z",
  "2022-01-13T04:00:00.000Z",
  "2022-01-14T04:00:00.000Z",
  "2022-01-17T04:00:00.000Z",
  "2022-01-18T04:00:00.000Z"
];

let app_multiple = dates.map(function combineTitleData(dataItem, index) {
  return {
    text: 'LR' + dates[index] + ': ' + names[index],
  };
});

console.log(app_multiple);

我想要实现的是使用“格式”数组是遵循模式并使用这个数组来构造映射。

格式包含:[3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];

我想在映射中使用这样的“名称”数组:

(chair) -> 3 times
(table) -> 3 times
(door) -> 1 time
(window) -> 5 times
(glass) -> 4 times
.... etc

所以输出会是这样的:

[
    {
        "text": "LR2021-10-04T04:00:00.000Z: chair"
    },
    {
        "text": "LR2021-10-05T04:00:00.000Z: chair"
    },
    {
        "text": "LR2021-10-06T04:00:00.000Z: chair"
    },
    {
        "text": "LR2021-10-13T04:00:00.000Z: table"
    },
    {
        "text": "LR2021-10-14T04:00:00.000Z: table"
    },
    {
        "text": "LR2021-10-15T04:00:00.000Z: table"
    },
    {
        "text": "LR2021-10-15T04:00:00.000Z: door"
    },
    {
        "text": "LR2021-10-17T22:00:00.000Z: window"
    },
    {
        "text": "LR2021-10-18T22:00:00.000Z: window"
    },
    {
        "text": "LR2021-10-19T22:00:00.000Z: window"
    },
    {
        "text": "LR2021-10-20T22:00:00.000Z: window"
    },
    {
        "text": "LR2021-10-21T22:00:00.000Z: window"
    }
    .... etc
    .... etc
]

这可能吗?谢谢。

【问题讨论】:

    标签: javascript arrays dictionary


    【解决方案1】:

    第一个选项是:

    var numTimesUsed = 0;
    var nameIndex = 0;
    let app_multiple = dates.map(function combineTitleData(dataItem, index) {
      if(format[nameIndex] == numTimesUsed) {
        nameIndex++;
        numTimesUsed = 0;
      }
      numTimesUsed++;
      return {
        text: 'LR' + dates[index] + ': ' + names[nameIndex],
       };
    });
    

    第二个选项是(我正在遍历名称映射而不是此选项的日期):

    var dateIndex = 0;
    let app_multiple = names.map(function combineTitleData(nameItem, index) {
      var datesForName = [];
      for(var i = 0; i < format[index]; i++) {
        datesForName[i] = {
          text: 'LR' + dates[dateIndex] + ': ' + names[index],
        };
        dateIndex++;
      }
      return datesForName;
    });
    

    您可能还可以使用一些数学来避免在第二个选项中出现额外的变量,但只使用额外的变量会更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多