-3

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 土' }
          ];
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    it looks like you want to correlate days to dates. please add your code. whot goes wrong? – Nina Scholz Nov 17 '21 at 14:53
  • Did you simply google that, before asking for help? Surely you're not the first person in the world who wants to merge two arrays. – Jeremy Thille Nov 17 '21 at 14:53
  • Welcome to Stack Overflow! Visit the [help], take the [tour] to see what and [ask]. Please first ***>>>[Search for related topics on SO](https://www.google.com/search?q=javascript+merge+two+object+arrays+site%3Astackoverflow.com)<<<*** and if you get stuck, post a [mcve] of your attempt, noting input and expected output using the [`[<>]`](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) snippet editor. – mplungjan Nov 17 '21 at 14:54
  • @JeremyThille After looking, it seems we all (including OP) misunderstood the word `merge` – mplungjan Nov 17 '21 at 15:06

3 Answers3

2

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)
dataTemp.forEach(d => d.calendar_date = `${d.calendar_date} ${listOrder[new Date(`${d.calendar_date} 15:00:00`).getDay()] }`);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

thanks all, my solution:

function getDay(dateString) {
  return {
    0: '日',
    1: '月',
    2: '火',
    3: '水',
    4: '木',
    5: '金',
    6: '土'
}[new Date(dateString).getDay()];

dataTemp.map(item => ({
 ...item,
 calendar_date_new: `${item.calendar_date} (${getDay(item.calendar_date)})`
}))
-2
let data = dataTemp.map((v,i) => {return {calendar_date: v.calendar_date + ' ' + listOrder[i % listOrder.length]}});
griffi-gh
  • 76
  • 5