-1

I have an array like this:

[
   [
     1, 2, 3, 4,
     5, 6, 7
   ],
   [
     { Id: 42258 },
     { Id: 42259 },
     { Id: 42260 },
     { Id: 42261 },
     { Id: 42262 },
     { Id: 42263 },
     { Id: 42264 },
     { Id: 42265 },
     { Id: 42266 },
   ],
[
         { Address_GetListCurrentResult: 'street1'  },
         { Address_GetListCurrentResult: 'street2' },
         { Address_GetListCurrentResult: 'street3'  },
         { Address_GetListCurrentResult: 'street4' },
         { Address_GetListCurrentResult: 'street5' },
         { Address_GetListCurrentResult: 'street6' },
         { Address_GetListCurrentResult:' street7' },
         { Address_GetListCurrentResult: 'street8' },
         { Address_GetListCurrentResult: 'street9' }
       ],
       [
         { PersonalInfo_GetCurrentResult: 'info1' },
         { PersonalInfo_GetCurrentResult: 'info2' },
         { PersonalInfo_GetCurrentResult: 'info3' },
         { PersonalInfo_GetCurrentResult: 'info4' },
         { PersonalInfo_GetCurrentResult: 'info5' },
         { PersonalInfo_GetCurrentResult: 'info6' },
         { PersonalInfo_GetCurrentResult: 'info7' },
         { PersonalInfo_GetCurrentResult: 'info8' },
         { PersonalInfo_GetCurrentResult: 'info9' }
       ]
 ]

how do i get all the objects at the same index in the nested arrays and get them into a new one with the index being dynamic.

Already tried to iterate with for and forEach but never goes into all of them, a code example of what i have right now

nmbrsEmployee = [
   [
     1, 2, 3, 4,
     5, 6, 7
   ],
   [
     { Id: 42258 },
     { Id: 42259 },
     { Id: 42260 },
     { Id: 42261 },
     { Id: 42262 },
     { Id: 42263 },
     { Id: 42264 },
     { Id: 42265 },
     { Id: 42266 },
   ],
   [
     { Address_GetListCurrentResult: 'street1'  },
     { Address_GetListCurrentResult: 'street2' },
     { Address_GetListCurrentResult: 'street3'  },
     { Address_GetListCurrentResult: 'street4' },
     { Address_GetListCurrentResult: 'street5' },
     { Address_GetListCurrentResult: 'street6' },
     { Address_GetListCurrentResult:' street7' },
     { Address_GetListCurrentResult: 'street8' },
     { Address_GetListCurrentResult: 'street9' }
   ],
   [
     { PersonalInfo_GetCurrentResult: 'info1' },
     { PersonalInfo_GetCurrentResult: 'info2' },
     { PersonalInfo_GetCurrentResult: 'info3' },
     { PersonalInfo_GetCurrentResult: 'info4' },
     { PersonalInfo_GetCurrentResult: 'info5' },
     { PersonalInfo_GetCurrentResult: 'info6' },
     { PersonalInfo_GetCurrentResult: 'info7' },
     { PersonalInfo_GetCurrentResult: 'info8' },
     { PersonalInfo_GetCurrentResult: 'info9' }
   ]
 ]
let objectsAtPosition = []
function testArray() {
  for (let i = 0; i < nmbrsEmployee.length; i++) {
      const subArray = nmbrsEmployee[i];
      const objectAtIndex = subArray[1];
      objectsAtPosition.push(objectAtIndex);
      console.log('objectsAtPosition', objectsAtPosition);
  }
}

Thank you for the help in advance

Keymaster
  • 27
  • 4
  • 1
    So what exactly is your problem? Find the item by the index, use the index in the other arrays. – epascarello Apr 17 '23 at 20:16
  • @epascarello my problem is findding the item by the index – Keymaster Apr 17 '23 at 20:22
  • duplicate: [How to merge each object within arrays by index?](https://stackoverflow.com/questions/50919164/how-to-merge-each-object-within-arrays-by-index) or [Combine arrays of objects by object index](https://stackoverflow.com/questions/66961324/combine-arrays-of-objects-by-object-index) – pilchard Apr 17 '23 at 22:01

1 Answers1

1

Assuming the input format is as follows:

  • a single array with at least two entries
  • the first entry contains an array of (0-based) indices into the remaining entries
  • all remaining entries are arrays of objects to be merged into one entry per index

const sampleData = [
   [
     1, 2, 3, 4,
     5, 6, 7
   ],
   [
     { Id: 42258 },
     { Id: 42259 },
     { Id: 42260 },
     { Id: 42261 },
     { Id: 42262 },
     { Id: 42263 },
     { Id: 42264 },
     { Id: 42265 },
     { Id: 42266 },
   ],
   [
     { Address_GetListCurrentResult: "street1"  },
     { Address_GetListCurrentResult: "street2" },
     { Address_GetListCurrentResult: "street3"  },
     { Address_GetListCurrentResult: "street4" },
     { Address_GetListCurrentResult: "street5" },
     { Address_GetListCurrentResult: "street6" },
     { Address_GetListCurrentResult: "street7" },
     { Address_GetListCurrentResult: "street8" },
     { Address_GetListCurrentResult: "street9" }
   ],
   [
     { PersonalInfo_GetCurrentResult: "info1" },
     { PersonalInfo_GetCurrentResult: "info2" },
     { PersonalInfo_GetCurrentResult: "info3" },
     { PersonalInfo_GetCurrentResult: "info4" },
     { PersonalInfo_GetCurrentResult: "info5" },
     { PersonalInfo_GetCurrentResult: "info6" },
     { PersonalInfo_GetCurrentResult: "info7" },
     { PersonalInfo_GetCurrentResult: "info8" },
     { PersonalInfo_GetCurrentResult: "info9" }
   ]
 ]


function mergeDataAtIndices([indices, ...data]) {
    const zipped = [];
   
    for (const index of indices) {
        const merged = {};
        for (const datum of data) {
            Object.assign(merged, datum[index]);
        }
        zipped.push(merged);
    }

    return zipped;
}

console.log(mergeDataAtIndices(sampleData));
motto
  • 2,888
  • 2
  • 2
  • 14
  • This log only 7 Objects/9 If you change const sampleData to const sampleData = [ [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ] You will have 9/9 elements and your snipped would work perfectly. – tatactic Apr 17 '23 at 21:30
  • Yes @tatactic - the data here contains the values from the original question (with strings in place of variable names) – motto Apr 17 '23 at 21:32
  • This isn't 'zipping' but simply merging by index – pilchard Apr 17 '23 at 22:02
  • @pilchard It's not quite merging by index but it's more than a `zip` isn't it, more of a "pick and zip". I'll rename it. – motto Apr 17 '23 at 22:06