Merge Array by one of its value

Viewed 78

I want to filter from two different arrays based on following array named = timeArray

["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]

first array looks like this .. array1

[
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]

second array looks like this .. array2

  [
 
    [23310,23310,23300,23300,0,0,"13:34"],
    [23309.75,23343.1,23305,23323.25,0,0,"13:35"],
    [23296.5,23310,23294.65,23309.8,0,0,"13:30"],
    [23308.35,23310,23301,23306.15,0,0,"13:31"],
    [23308,23309,23292.5,23299.55,0,0,"13:32"],
    [23299.55,23310,23294.15,23310,0,0,"13:33"],
    [23310,23310,23300,23300,0,0,"13:34"],
    [23309.75,23343.1,23305,23324.65,0,0,"13:35"],
    [23308,23309,23292.5,23299.55,0,0,"13:36"]
    ]

Based on Timearray elements, First should check array1 and then array 2 6th element and expected result should look like

  [
    [23308,23309,23292.5,23299.55,0,0,"13:32"],
    [23299.55,23310,23294.15,23310,0,0,"13:33"],
    [23310,23310,23300,23300,0,0,"13:34"],
    [23309.75,23343.1,23305,23324.65,0,0,"13:35"],
    [23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
    [23315.05,23315.2,23314,23315,8,9,"13:37"]
 ]

I tried something like below

var finalarray = [];
for(var key in timeArray)
{
  var timer = timeArray[key];
  for(var key in array1)
  {
    arraytime1 = array1[key][6];
    if(timer == arraytime1)
    {
      finalarray.push(arraytime1);
    }
  }

  for(var key in array2)
  {
    arraytime2 = array2[key][6];
    if(timer == arraytime2)
    {
      finalarray.push(arraytime2);
    }
  }
}

But "13:36" is added two times...Also if the element based on timerArray is not present it should be null... also is there any better way to do it ? I feel like it takes much resource to process... Thank you.

5 Answers

I believe this is what you want

const base = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
const arrayA = [
  [23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
  [23315.05,23315.2,23314,23315,8,9,"13:37"]
]
const arrayB = [
  [23310,23310,23300,23300,0,0,"13:34"],
  [23309.75,23343.1,23305,23323.25,0,0,"13:35"],
  [23296.5,23310,23294.65,23309.8,0,0,"13:30"],
  [23308.35,23310,23301,23306.15,0,0,"13:31"],
  [23308,23309,23292.5,23299.55,0,0,"13:32"],
  [23299.55,23310,23294.15,23310,0,0,"13:33"],
  [23310,23310,23300,23300,0,0,"13:34"],
  [23309.75,23343.1,23305,23324.65,0,0,"13:35"],
  [23308,23309,23292.5,23299.55,0,0,"13:36"]
]

// const expectedResult = [
//  [23308,23309,23292.5,23299.55,0,0,"13:32"],
//  [23299.55,23310,23294.15,23310,0,0,"13:33"],
//  [23310,23310,23300,23300,0,0,"13:34"],
//  [23309.75,23343.1,23305,23324.65,0,0,"13:35"],
//  [23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
//  [23315.05,23315.2,23314,23315,8,9,"13:37"]
//]

const res = base.sort().map(time => {
  return arrayA.find(subArray => subArray[subArray.length - 1] === time)
    || arrayB.find(subArray => subArray[subArray.length - 1] === time)
    || null
});

console.log(res);

I would modify the arrays in the following format to obtain a much faster access:

{
  "13:34": [23310,23310,23300,23300,0,0]
}

Then find the appropriate array for each time item.

Code:

const timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]

const array1 = [
  [23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
  [23315.05,23315.2,23314,23315,8,9,"13:37"]
]

const array2 = [
  [23310,23310,23300,23300,0,0,"13:34"],
  [23309.75,23343.1,23305,23323.25,0,0,"13:35"],
  [23296.5,23310,23294.65,23309.8,0,0,"13:30"],
  [23308.35,23310,23301,23306.15,0,0,"13:31"],
  [23308,23309,23292.5,23299.55,0,0,"13:32"],
  [23299.55,23310,23294.15,23310,0,0,"13:33"],
  [23310,23310,23300,23300,0,0,"13:34"],
  [23309.75,23343.1,23305,23324.65,0,0,"13:35"],
  [23308,23309,23292.5,23299.55,0,0,"13:36"]
]


const values1 = {}
array1.forEach(item => {
   values1[item[6]] = item
})

const values2 = {}
array2.forEach(item => {
   values2[item[6]] = item
})

const result = []
timeArray.sort().forEach(time => {
   let itemResult = [0,0,0,0,0,0, time]
   if (values1[time]) {
     itemResult = values1[time]

   } else if (values2[time]) {
     itemResult = values2[time]

   }
   result.push(itemResult)
})

console.log(result)

You could take an object for keeping the fist array for each time and map the result.

const
    timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"],
    array1 = [[23323.25, 23323.65, 23313.25, 23315.05, 97, 62, "13:36"], [23315.05, 23315.2, 23314, 23315, 8, 9, "13:37"]],
    array2 = [[23310, 23310, 23300, 23300, 0, 0, "13:34"], [23309.75, 23343.1, 23305, 23323.25, 0, 0, "13:35"], [23296.5, 23310, 23294.65, 23309.8, 0, 0, "13:30"], [23308.35, 23310, 23301, 23306.15, 0, 0, "13:31"], [23308, 23309, 23292.5, 23299.55, 0, 0, "13:32"], [23299.55, 23310, 23294.15, 23310, 0, 0, "13:33"], [23310, 23310, 23300, 23300, 0, 0, "13:34"], [23309.75, 23343.1, 23305, 23324.65, 0, 0, "13:35"], [23308, 23309, 23292.5, 23299.55, 0, 0, "13:36"]],
    times = [array1, array2].reduce((r, array) => {
        array.forEach(a => r[a[6]] ??= a)
        return r;
    }, {}),
    result = timeArray
        .sort()
        .map(time => times[time] || [0, 0, 0, 0, 0, time]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

in my opinion you can do like this

var finalarray = [];
for(var key in timeArray)
{
    var timer = timeArray[key];
    for(var key in array2)
    {
        arraytime2 = array2[key][6];
        if(timer == arraytime2)
        {
           finalarray[arraytime2]=array2[key];
        }
    }

    for(var key in array1)
    {
        arraytime1 = array1[key][6];
        if(timer == arraytime1)
        {
            finalarray[arraytime1]=array1[key];
        }
    }

}

It is possible to use Map collection to have O(1) while mapping the items. So we can sort timeArray and then just map() items:

const unique_1 = new Map(array1.map(s => [s[6], s]));
const unique_2 = new Map(array2.map(s => [s[6], s]));

const result = timeArray
                  .sort()
                  .map(time => unique_1.get(time) || unique_2.get(time));

An example:

const timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]

const array1 = [
  [23323.25, 23323.65, 23313.25, 23315.05, 97, 62, "13:36"],
  [23315.05, 23315.2, 23314, 23315, 8, 9, "13:37"]
]

const array2 = [
  [23310, 23310, 23300, 23300, 0, 0, "13:34"],
  [23309.75, 23343.1, 23305, 23323.25, 0, 0, "13:35"],
  [23296.5, 23310, 23294.65, 23309.8, 0, 0, "13:30"],
  [23308.35, 23310, 23301, 23306.15, 0, 0, "13:31"],
  [23308, 23309, 23292.5, 23299.55, 0, 0, "13:32"],
  [23299.55, 23310, 23294.15, 23310, 0, 0, "13:33"],
  [23310, 23310, 23300, 23300, 0, 0, "13:34"],
  [23309.75, 23343.1, 23305, 23324.65, 0, 0, "13:35"],
  [23308, 23309, 23292.5, 23299.55, 0, 0, "13:36"]
]

const unique_1 = new Map(array1.map(s => [s[6], s]));
const unique_2 = new Map(array2.map(s => [s[6], s]));

const result = timeArray
                  .sort()
                  .map(time => unique_1.get(time) || unique_2.get(time));

console.log(result)

Related