Skip to main content
Bumped by Community user
Capitalize personal pronouns, add tag
Source Link

I have three arrays in which first index is same, so iI want to merge all three array into one array based on first index element

Input:

[[1, 'A'], [2, 'B'], [3, 'C']];
[[1, 'D'], [2, 'E'], [3, 'F']];
[[1, 'G'], [2, 'H'], [3, 'I']];

Expected output

[ 
  [ 1, 'A', 'D', 'G' ], 
  [ 2, 'B', 'E', 'H' ], 
  [ 3, 'C', 'F', 'I' ] 
]

My code:

function mergeArrays(arrays) {
  const mergedMap = new Map();

  for (const array of arrays) {
    for (const item of array) {
      const key = item[0];
      if (!mergedMap.has(key)) {
        mergedMap.set(key, [key]);
      }
      mergedMap.get(key).push(...item.slice(1));
    }
  }

  const mergedArray = Array.from(mergedMap.values());

  return mergedArray;
}

const array1 = [[1, 'A'], [2, 'B'], [3, 'C']];
const array2 = [[1, 'D'], [2, 'E'], [3, 'F']];
const array3 = [[1, 'G'], [2, 'H'], [3, 'I']];

const mergedResult = mergeArrays([array1, array2, array3]);
console.log(mergedResult);

Would you please suggest any better solution?

Please note: for the simplicity iI have make first index as number but in my case it is a Date object.

I have three arrays in which first index is same, so i want to merge all three array into one array based on first index element

Input:

[[1, 'A'], [2, 'B'], [3, 'C']];
[[1, 'D'], [2, 'E'], [3, 'F']];
[[1, 'G'], [2, 'H'], [3, 'I']];

Expected output

[ 
  [ 1, 'A', 'D', 'G' ], 
  [ 2, 'B', 'E', 'H' ], 
  [ 3, 'C', 'F', 'I' ] 
]

My code:

function mergeArrays(arrays) {
  const mergedMap = new Map();

  for (const array of arrays) {
    for (const item of array) {
      const key = item[0];
      if (!mergedMap.has(key)) {
        mergedMap.set(key, [key]);
      }
      mergedMap.get(key).push(...item.slice(1));
    }
  }

  const mergedArray = Array.from(mergedMap.values());

  return mergedArray;
}

const array1 = [[1, 'A'], [2, 'B'], [3, 'C']];
const array2 = [[1, 'D'], [2, 'E'], [3, 'F']];
const array3 = [[1, 'G'], [2, 'H'], [3, 'I']];

const mergedResult = mergeArrays([array1, array2, array3]);
console.log(mergedResult);

Would you please suggest any better solution?

Please note: for the simplicity i have make first index as number but in my case it is a Date object.

I have three arrays in which first index is same, so I want to merge all three array into one array based on first index element

Input:

[[1, 'A'], [2, 'B'], [3, 'C']];
[[1, 'D'], [2, 'E'], [3, 'F']];
[[1, 'G'], [2, 'H'], [3, 'I']];

Expected output

[ 
  [ 1, 'A', 'D', 'G' ], 
  [ 2, 'B', 'E', 'H' ], 
  [ 3, 'C', 'F', 'I' ] 
]

My code:

function mergeArrays(arrays) {
  const mergedMap = new Map();

  for (const array of arrays) {
    for (const item of array) {
      const key = item[0];
      if (!mergedMap.has(key)) {
        mergedMap.set(key, [key]);
      }
      mergedMap.get(key).push(...item.slice(1));
    }
  }

  const mergedArray = Array.from(mergedMap.values());

  return mergedArray;
}

const array1 = [[1, 'A'], [2, 'B'], [3, 'C']];
const array2 = [[1, 'D'], [2, 'E'], [3, 'F']];
const array3 = [[1, 'G'], [2, 'H'], [3, 'I']];

const mergedResult = mergeArrays([array1, array2, array3]);
console.log(mergedResult);

Would you please suggest any better solution?

Please note: for the simplicity I have make first index as number but in my case it is a Date object.

Source Link

Merge three (or more) arrays based on specific index and create new array

I have three arrays in which first index is same, so i want to merge all three array into one array based on first index element

Input:

[[1, 'A'], [2, 'B'], [3, 'C']];
[[1, 'D'], [2, 'E'], [3, 'F']];
[[1, 'G'], [2, 'H'], [3, 'I']];

Expected output

[ 
  [ 1, 'A', 'D', 'G' ], 
  [ 2, 'B', 'E', 'H' ], 
  [ 3, 'C', 'F', 'I' ] 
]

My code:

function mergeArrays(arrays) {
  const mergedMap = new Map();

  for (const array of arrays) {
    for (const item of array) {
      const key = item[0];
      if (!mergedMap.has(key)) {
        mergedMap.set(key, [key]);
      }
      mergedMap.get(key).push(...item.slice(1));
    }
  }

  const mergedArray = Array.from(mergedMap.values());

  return mergedArray;
}

const array1 = [[1, 'A'], [2, 'B'], [3, 'C']];
const array2 = [[1, 'D'], [2, 'E'], [3, 'F']];
const array3 = [[1, 'G'], [2, 'H'], [3, 'I']];

const mergedResult = mergeArrays([array1, array2, array3]);
console.log(mergedResult);

Would you please suggest any better solution?

Please note: for the simplicity i have make first index as number but in my case it is a Date object.