简体   繁体   中英

How to combine two javascript array into one and group by date

Hello I'am new to programming and I stumble upon on grouping array data by date from two arrays.

here is my arrays:

header = [
    {"2019-04-22": "Sun, Apr 22, 2019"},
    {"2019-04-21": "Sat, Apr 21, 2019"},
]

body = [
    {"2019-04-22": "doing customer support”},
    {"2019-04-22": "reply to emails"},
    {"2019-04-21": "send message to customers"},
]

How do I group the arrays into one array as example below

combinearray = {
    "2019-04-22": [
        "Sun, Apr 22, 2019",
        "doing customer support",
        "reply to emails",
    ],
    "2019-04-21": [
        "Sat, Apr 21, 2019",
        "send message to customers",
    ],
}

Grouping two array data by date seems completely not easy for me I'm a beginner to javascript programming. I would appreciate any answers.

You can do that in following steps:

  • First use concat() to combine both arrays ie header and body
  • Then use reduce() on that. And pass empty object as second argument(the initial value of accumulator).
  • In inside reduce() callback use Object.keys()[0] to get date.
  • Check if the date if date is not already key of accumulator set it to empty [] .
  • Use push() to add the elements to the array.

Note : This will not remove reference to the real object in header and body .

 const header = [ {"2019-04-22": "Sun, Apr 22, 2019"}, {"2019-04-21": "Sat, Apr 21, 2019"} ] const body = [ {"2019-04-22": "doing customer support"}, {"2019-04-22": "reply to emails"}, {"2019-04-21": "send message to customers"}, ] const res = header.concat(body).reduce((ac,a) => { let key = Object.keys(a)[0]; ac[key] = ac[key] || []; ac[key].push(a) return ac; },{}) console.log(res) 

However as mentioned in the comments there is no need to have object with keys. Just simple array of the values of that key are enough. For that push() a[key] instead of a .

 const header = [ {"2019-04-22": "Sun, Apr 22, 2019"}, {"2019-04-21": "Sat, Apr 21, 2019"} ] const body = [ {"2019-04-22": "doing customer support"}, {"2019-04-22": "reply to emails"}, {"2019-04-21": "send message to customers"}, ] const res = header.concat(body).reduce((ac,a) => { let key = Object.keys(a)[0]; ac[key] = ac[key] || []; ac[key].push(a[key]) return ac; },{}) console.log(res) 

You can use combine arrays then use reduce

  • used spread syntax to merge arrays
  • use reduce to build an object in desired format
  • Object.entries to get date and it's respective value
  • Check if the date is already present as key on object or not, if it's already present push the value to it else create a new key

 let header = [{"2019-04-22": "Sun, Apr 22, 2019"},{"2019-04-21": "Sat, Apr 21, 2019"},] let body = [{"2019-04-22": "doing customer support"},{"2019-04-22": "reply to emails"},{"2019-04-21": "send message to customers"},] let final = [...header,...body].reduce((op,inp) => { let [key,value] = Object.entries(inp)[0] op[key] = op[key] || [] op[key].push(value) return op },{}) console.log(final) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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