Home > Net >  How I can assign a custom format to an array?
How I can assign a custom format to an array?

Time:10-27

I have an array of dates that I use for mapping. When trying to pass items from a different array (names) using the index, I get undefined values because the "names" array has less entries than the index (dates).

I have a third array with what should be the correct format (format):

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

What I would like to achieve is using the "format" array is to follow the patern and use this array to construct the mapping.

The format contains: [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];

I would like to use in the mapping the "names" array like this:

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

So the output would be like this:

[
    {
        "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
]

Is this possible to do? Thanks.

CodePudding user response:

The first option is:

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

The second option is (I'm looping through the names map instead of dates for this option):

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

You could probably also use some math to avoid extra variables in the second option, but its easier to just have an extra variable.

  • Related