October 23, 2024
Chicago 12, Melborne City, USA
javascript

pair numbers from multiple arrays


Trying to sort some data from a array of arrays which could be different every time.

ie:

data = [
   [ null, 1000, null, null, null ],
   [ 2000, null, 3000, 4000, 5000 ]
]

What i am trying to get out of this array is a set of array pairs, the first value in the first array is null and should be paired in new array with 2000, then 1000 pair is null and should paired as separate array as well and so on.

Expected Pairs

Expected Result:

data = [
   [null, 2000],
   [1000, null],
   [null, 3000],
   [null, 4000],
   [null, 5000],
]

the actual data set can be larger in any direction ie:

data = [
   [ null, 1000, null, null, null, 6000, 7000  ],
   [ 2000, null, 3000, 4000, 5000, null, null  ]
   [ 3423, null, 4545, 1435, 9898, null, 7643  ]
]

now the expected pairs would be

data = [
   [null, 2000, 3423],
   [1000, null, null],
   [null, 3000, 4545],
   [null, 4000, 1435],
   [null, 5000, 9898],
   [6000, null, null],
   [7000, null, 7643],
]

I know its a multidimensional array so i am looping over the data set then over each data array from that data set and trying to push each number into a new array, but all i can achieve is a flat version of the data ie:

data: [
    [
        null
    ],
    [
        1000
    ],
    [
        null
    ],
    [
        null
    ],
    [
        null
    ],
    [
        2000
    ],
    [
        null
    ],
    [
        3000
    ],
    [
        4000
    ],
    [
        5000
    ]
]

My Code:

  const test = [
    [null, 1000, null, null, null],
    [2000, null, 3000, 4000, 5000]
  ];

  const testFunc = dataSet => {
    const lineChartData = [];
    dataSet.map(numbersSet =>
      numbersSet.map(number => lineChartData.push([number]))
    );
    return lineChartData;
  };

  console.log("TEST", testFunc(test));



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video