Home > front end >  How to add to an array entry based on day number to array
How to add to an array entry based on day number to array

Time:11-18

I have created 2 arrays and I want to add the variable in listOrder to each date in dataTemp based on day.

let listOrder = ['日', '月', '火', '水', '木', '金', '土'];
let dataTemp = [
            { calendar_date: '2021/11/7' },
            { calendar_date: '2021/11/8' },
            { calendar_date: '2021/11/9' },
            { calendar_date: '2021/11/10' },
            { calendar_date: '2021/11/11' },
            { calendar_date: '2021/11/12' },
            { calendar_date: '2021/11/13' },
            { calendar_date: '2021/11/14' },
            { calendar_date: '2021/11/15' },
            { calendar_date: '2021/11/16' },
            { calendar_date: '2021/11/17' },
            { calendar_date: '2021/11/18' },
            { calendar_date: '2021/11/19' },
            { calendar_date: '2021/11/20' }
          ];

I want to merge the 2 arrays of objects into one like the following:

let data = [
            { calendar_date: '2021/11/7 日' },
            { calendar_date: '2021/11/8 月' },
            { calendar_date: '2021/11/9 火' },
            { calendar_date: '2021/11/10 水' },
            { calendar_date: '2021/11/11 木' },
            { calendar_date: '2021/11/12 金' },
            { calendar_date: '2021/11/13 土' },
            { calendar_date: '2021/11/14 日' },
            { calendar_date: '2021/11/15 月' },
            { calendar_date: '2021/11/16 火' },
            { calendar_date: '2021/11/17 水' },
            { calendar_date: '2021/11/18 木' },
            { calendar_date: '2021/11/19 金' },
            { calendar_date: '2021/11/20 土' }
          ];

CodePudding user response:

Initially it was not easy to understand your question.

I have reworded it and here is the answer to that version of the question

I assume the listOrder are names of days from Sunday to Saturday

I also decided to normalise on time to make sure daylight savings or times around GMT would not matter

https://jsfiddle.net/mplungjan/s3vxbnuz

let listOrder = ['日', '月', '火', '水', '木', '金', '土'];
let dataTemp = [
            { calendar_date: '2021/11/7' },
            { calendar_date: '2021/11/8' },
            { calendar_date: '2021/11/9' },
            { calendar_date: '2021/11/10' },
            { calendar_date: '2021/11/11' },
            { calendar_date: '2021/11/12' },
            { calendar_date: '2021/11/13' },
            { calendar_date: '2021/11/14' },
            { calendar_date: '2021/11/15' },
            { calendar_date: '2021/11/16' },
            { calendar_date: '2021/11/17' },
            { calendar_date: '2021/11/18' },
            { calendar_date: '2021/11/19' },
            { calendar_date: '2021/11/20' }
          ];

          
dataTemp.forEach(d => d.calendar_date = `${d.calendar_date} ${listOrder[new Date(`${d.calendar_date} 15:00:00`).getDay()]}`)          
console.log(dataTemp)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

dataTemp.forEach(d => d.calendar_date = `${d.calendar_date} ${listOrder[new Date(`${d.calendar_date} 15:00:00`).getDay()] }`);

CodePudding user response:

let data = dataTemp.map((v,i) => {return {calendar_date: v.calendar_date   ' '   listOrder[i % listOrder.length]}});
  • Related