最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

binearray = {
    "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 pletely not easy for me I'm a beginner to javascript programming. I would appreciate any answers.

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

binearray = {
    "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 pletely not easy for me I'm a beginner to javascript programming. I would appreciate any answers.

Share Improve this question edited Apr 27, 2019 at 7:06 Pro Dev asked Apr 27, 2019 at 4:06 Pro DevPro Dev 6841 gold badge9 silver badges28 bronze badges 7
  • 1 binearray should be obj wrapped in {} instead of [] – Maheer Ali Commented Apr 27, 2019 at 4:07
  • 1 Your desired output is not valid array – Code Maniac Commented Apr 27, 2019 at 4:07
  • sorry for that. – Pro Dev Commented Apr 27, 2019 at 4:10
  • 1 The desired output seems like a slight smell to me. Is there any reason that each date is an array of objects, rather than just "2019-04-21": [ "Sat, Apr 21, 2019", "send message to customers"]? You repeat the key for every single object - what is the point of an object if the only portion you care about is the string? – Tyler Roper Commented Apr 27, 2019 at 4:14
  • 3 I'm asking why you want an object structure that nests and repeats data where it doesn't seem to be at all necessary. I understand what you want, I just think you should consider it a bit. If you have a good reason for it, then sure, just pointing out that it seems redundant. You've made an array of key-value pairs where the key (seemingly) serves no purpose. – Tyler Roper Commented Apr 27, 2019 at 4:18
 |  Show 2 more ments

2 Answers 2

Reset to default 4

You can do that in following steps:

  • First use concat() to bine both arrays i.e 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 ments 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 bine 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)

发布评论

评论列表(0)

  1. 暂无评论