2

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.

6
  • 2
    You didn't explain how the result array should be calculated from the first two array. If there is a match in both array1 and array2, which one should you add to the result? Commented Oct 19, 2020 at 14:36
  • please add stacksnippets to show the actual result. Commented Oct 19, 2020 at 14:37
  • @gbalduzzi , it is based on the timeArray elements and 6th elements of first two arrays Commented Oct 19, 2020 at 14:39
  • I clearly understand that you can match the elements from array1 and array2 to the timeArray looking at the 6th element. But it is not clear what you should do if the same time reference is present in both array1 and array2, as 13.36 in your example. You should keep the one in array1? You should sum them? You should keep the bigger one? I don't know Commented Oct 19, 2020 at 14:42
  • please add the wanted result. btw, it is not advisable to iterate an array with for ... in loop instead of takinging elements with either an index or with for ...of. Commented Oct 19, 2020 at 14:42

5 Answers 5

2

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; }

Sign up to request clarification or add additional context in comments.

Comments

2

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);

2 Comments

While this solution works, it is absolutely not efficient. You have a time complexity of O(n^3)
The only way to make it faster with this input is directly check the last element of the subArrays, which I can edit. Other than that, it wont be better with array inputs
2

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)

Comments

2

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];
        }
    }

}

Comments

1

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)

2 Comments

What is the advantage of `${s[6]}` instead of simply s[6] ?
@gbalduzzi there is no advantage, I just thought that the type of the 6 element is not string. Thanks for the great comment. I've edited my reply.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.