I have two JavaScript arrays of strings as a result from a SQL query (each array is one column in a result) which may contain duplicities like:
arrOne = ["key_1", "key_1", "key_2", "key_3", "key_1", "key_1"]
arrTwo = ["val_1", "val_1", "val_3", "val_3", "val_2", "val_3"]
and I need to reduce them into an object like:
newObj = {
key_1: ["val_1", "val_2", "val_3"],
key_2: ["val_3"],
key_3: ["val_3"]
}
So basically, for each key, value pair, the value should be a reduced list of unique values from an arrTwo.
I have tried:
let newObj = arrOne.reduce((o, k, i) => ({...o, [k]: arrTwo[i]}), {})
which gives me:
{ key_1: 'val_3', key_2: 'val_3', key_3: 'val_3' }
but I need arrTwo[i] part to be reduced list of values instead of last value for each key.
This may have an elegant one-line solution but I am new in JavaScript and I can't find the proper solution (I think a loop is not necessary here).