0

const arrays = [
  [123, "string1"],
  [4564564, "string2"],
  [392341231, "string3"],
  [1665342, "String4", 334934543, "string5"]
];

const s = arrays.reduce((acc, val) => {
  return acc.concat(val);
});

// output: 
// [123, "string1", 4564564, "string2", 392341231, "string3", 1665342, "String4", 334934543, "string5"]

After flatten the Array, the output is one number one string, I was trying to use reduce to make the an object like

{
123: "string1",
4564564: "string2",
392341231: "string3",
1665342:  "String4",
334934543: "string5"
}
.reduce(function(acc, cur, i) {
  acc[cur] = cur;
  return acc;
}, {});

The output is wrong, how can I separate number and string inside reduce? Thanks

olo
  • 5,225
  • 15
  • 52
  • 92

4 Answers4

1

You could do it easily enough by mapping the individual indexes and assigning them all to a new object, which would reduce your need for an intermediary step

const arrays = [
  [123, "string1"],
  [4564564, "string2"],
  [392341231, "string3"],
  [1665342, "String4", 334934543, "string5"]
];

console.log( Object.assign( {}, ...arrays.map( i => ({[i[0]]: i[1] }) ) ) );
Icepickle
  • 12,689
  • 3
  • 34
  • 48
1

You need to keep both the key and the value. Iterating over each subarray, assigning the second item (the value) onto the accumulator at the first item (the key):

const arrays = [
  [123, "string1"],
  [4564564, "string2"],
  [392341231, "string3"],
  [1665342, "String4", 334934543, "string5"]
];

const obj = arrays.reduce((a, subarr) => {
  for (let i = 0; i < subarr.length; i += 2) {
    a[subarr[i]] = subarr[i + 1];
  }
  return a;
}, {});
console.log(obj);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

You can do it like this:

const arrays = [
  [123, "string1"],
  [4564564, "string2"],
  [392341231, "string3"],
  [1665342, "String4", 334934543, "string5"]
];

const s = arrays.reduce((acc, val) => {
  let i = 0
  while (i < val.length) {
    acc[val[i]] = val[i + 1];
    i += 2
  }
  return acc;
},{});

console.log( s )
cccn
  • 929
  • 1
  • 8
  • 20
  • Thanks for the answer, good idea. however, this snippet doesn't finish the last `array [1665342, "String4", 334934543, "string5"]` – olo Aug 19 '19 at 23:24
  • 1
    @olo i've missed the last one.Just edited my answer. Out of curiosity i was trying to search some info about which loop is faster and found [this discussion](i've missed the last one. Just edited my answer. Out of curiosity i was trying to search some info about which loop is faster and found this [discussion](https://stackoverflow.com/questions/18640032/javascript-performance-while-vs-for-loops) - duh :) – cccn Aug 20 '19 at 00:17
0

Here is more traditional and compatible way which supports all of the browsers. :)

var testArray = [
  [123, "string1"],
  [4564564, "string2"],
  [392341231, "string3"],
  [1665342, "String4", 334934543, "string5"]
];

var obj = {};
for (i = 0; i < testArray.length; i++) {
    for (j = 0; j < testArray[i].length; j+=2) {
        if (typeof testArray[i][j] === 'number' && typeof testArray[i][j+1] === 'string') {
            var number = testArray[i][j];
            var string = testArray[i][j+1];
            obj[number] = string;
 }
    }
}

console.log(obj);
Umair Shah
  • 2,305
  • 2
  • 25
  • 50