user6332379
user6332379

Reputation:

Filtering an array with multiple conditions

I got two arrays with some objects which I receive from API calls.

I need to combine those in one single array, but some objects are in different states on my app. So, if the objects in both array got the same "Code" attribute, I need to keep the one with more information.

I tried to make and nested loop (yeah, from what I read it's a poor technique, but I will never get more than 5 items at the same time). Then I tried to compare both "code objects", and push the equal results on a new array, which works fine. When I try to use a second condition on the same loop, it just doesn't work.

let array1 = [{
    'DataCad': "2019-01-04T15:04:02.663",
    'Field1': "rt",
    'Code': "DFG3456",
  },
  {
    'DataCad': "2019-01-07T11:37:31.8",
    'Field1': "TESTE2",
    'Code': "TYU1235",
  },
  {
    'DataCad': "2019-01-07T13:15:48.97",
    'Field1': "Uppercase",
    'Code': "JJJ1212",
  },
  {
    'DataCad': "2019-01-07T16:35:32.697",
    'Field1': "234",
    'Code': "OOO1111",
  },
  {
    'DataCad': "2019-01-07T10:46:46.437",
    'Field1': "TESTE1",
    'Code': "GHJ1234",
  }
]

let array2 = [{
    'DataAge': "2019-01-07",
    'DataCad': "2019-01-04T15:04:49.05",
    'HoraAge': "12:25",
    'Field1': "rt",
    'Pag': "N",
    'Code': "DFG3456",
  },
  {
    'DataAge': "2019-01-07",
    'DataCad': "2019-01-07T11:17:57.583",
    'HoraAge': "13:15",
    'Field1': "TESTE1",
    'Pag': "N",
    'Code': "GHJ1234",
  },
  {
    'DataAge': "2019-01-08",
    'DataCad': "2019-01-07T11:38:46.08",
    'HoraAge': "10:15",
    'Field1': "TESTE2",
    'Pag': "N",
    'Code': "TYU1235",
  },
  {
    'DataAge': "2020-01-7 ",
    'DataCad': "2019-01-07T13:16:00.567",
    'HoraAge': "15:15",
    'Field1': "Uppercase",
    'Pag': "N",
    'Code': "JJJ1212",
  }
]

let finalArray = [];


for (var i in array1) {

  for (var j in array2) {
    if (array1[i].Code == array2[j].Code && !array1[i].DataAge) {
      finalArray.push(array1[i].Code)
    }
  }
}

console.log(finalArray);
}

Need to get one single array with those filtered results. Is there a better solution?

Upvotes: 1

Views: 91

Answers (2)

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48337

You could use map method by passing a provided callback function as argument which is applied for every item from your given array.

Also, use find method in order to make Code property matching.

I need to keep the one with more information.

Use Object.keys method in order to decide which object contains more information.

let array1 = [ { 'DataCad': "2019-01-04T15:04:02.663", 'Field1': "rt", 'Code': "DFG3456", }, { 'DataCad': "2019-01-07T11:37:31.8", 'Field1': "TESTE2", 'Code': "TYU1235", }, { 'DataCad': "2019-01-07T13:15:48.97", 'Field1': "Uppercase", 'Code': "JJJ1212", }, { 'DataCad': "2019-01-07T16:35:32.697", 'Field1': "234", 'Code': "OOO1111", } , { 'DataCad': "2019-01-07T10:46:46.437", 'Field1': "TESTE1", 'Code': "GHJ1234", } ]

let array2 = [ { 'DataAge': "2019-01-07", 'DataCad': "2019-01-04T15:04:49.05", 'HoraAge': "12:25", 'Field1': "rt", 'Pag': "N", 'Code': "DFG3456", }, { 'DataAge': "2019-01-07", 'DataCad': "2019-01-07T11:17:57.583", 'HoraAge': "13:15", 'Field1': "TESTE1", 'Pag': "N", 'Code': "GHJ1234", }, { 'DataAge': "2019-01-08", 'DataCad': "2019-01-07T11:38:46.08", 'HoraAge': "10:15", 'Field1': "TESTE2", 'Pag': "N", 'Code': "TYU1235", }, { 'DataAge': "2020-01-7 ", 'DataCad': "2019-01-07T13:16:00.567", 'HoraAge': "15:15", 'Field1': "Uppercase", 'Pag': "N", 'Code': "JJJ1212", } ]

let finalArray = array1.map(function(item){
   let foundItem = array2.find(({Code}) => Code == item.Code);
   if(foundItem)
      return Object.keys(item).length > Object.keys(foundItem).length ? item : foundItem;
   return item;
});
console.log(finalArray);

Here's a short hand and slightly more functional approach too, this approach makes use of techniques such as currying and whatnot, if you'd like to look into functional programming, maybe look up Eric Elliot.

let array1 = [ { 'DataCad': "2019-01-04T15:04:02.663", 'Field1': "rt", 'Code': "DFG3456", }, { 'DataCad': "2019-01-07T11:37:31.8", 'Field1': "TESTE2", 'Code': "TYU1235", }, { 'DataCad': "2019-01-07T13:15:48.97", 'Field1': "Uppercase", 'Code': "JJJ1212", }, { 'DataCad': "2019-01-07T16:35:32.697", 'Field1': "234", 'Code': "OOO1111", } , { 'DataCad': "2019-01-07T10:46:46.437", 'Field1': "TESTE1", 'Code': "GHJ1234", } ]

let array2 = [ { 'DataAge': "2019-01-07", 'DataCad': "2019-01-04T15:04:49.05", 'HoraAge': "12:25", 'Field1': "rt", 'Pag': "N", 'Code': "DFG3456", }, { 'DataAge': "2019-01-07", 'DataCad': "2019-01-07T11:17:57.583", 'HoraAge': "13:15", 'Field1': "TESTE1", 'Pag': "N", 'Code': "GHJ1234", }, { 'DataAge': "2019-01-08", 'DataCad': "2019-01-07T11:38:46.08", 'HoraAge': "10:15", 'Field1': "TESTE2", 'Pag': "N", 'Code': "TYU1235", }, { 'DataAge': "2020-01-7 ", 'DataCad': "2019-01-07T13:16:00.567", 'HoraAge': "15:15", 'Field1': "Uppercase", 'Pag': "N", 'Code': "JJJ1212", } ];

// Finds an object in the givne array based on the code property. 
let find = i => a => a.find(({Code}) => Code == i.Code)

// Returns the object with more keys.
let longer = i => o => Object.keys(i).length > Object.keys(o).length ? i : o;

// Returns the relevant array of objects.
let filter = a1 => a2 => a1.map(i => find(i)(a2) ? longer(i)(find(i)(a2)) :  i);

// Fire the filter function.
let finalArray = filter(array1)(array2);

console.log(finalArray); // Log it!

Upvotes: 3

Ans Bilal
Ans Bilal

Reputation: 1067

You can iteterate over array1 and find the matching element in array2 and then push the element with more information (with more number of properties) in your final array.

let finalArray = [];

let array1 = [
    {
        'DataCad': "2019-01-04T15:04:02.663",
        'Field1': "rt",
        'Code': "DFG3456",
    },
    {
        'DataCad': "2019-01-07T11:37:31.8",
        'Field1': "TESTE2",
        'Code': "TYU1235",
    },
    {
        'DataCad': "2019-01-07T13:15:48.97",
        'Field1': "Uppercase",
        'Code': "JJJ1212",
    },
    {
        'DataCad': "2019-01-07T16:35:32.697",
        'Field1': "234",
        'Code': "OOO1111",
    },
    {
        'DataCad': "2019-01-07T10:46:46.437",
        'Field1': "TESTE1",
        'Code': "GHJ1234",
    }
]

let array2 = [
    {
        'DataAge': "2019-01-07",
        'DataCad': "2019-01-04T15:04:49.05",
        'HoraAge': "12:25",
        'Field1': "rt",
        'Pag': "N",
        'Code': "DFG3456",
    },
    {
        'DataAge': "2019-01-07",
        'DataCad': "2019-01-07T11:17:57.583",
        'HoraAge': "13:15",
        'Field1': "TESTE1",
        'Pag': "N",
        'Code': "GHJ1234",
    },
    {
        'DataAge': "2019-01-08",
        'DataCad': "2019-01-07T11:38:46.08",
        'HoraAge': "10:15",
        'Field1': "TESTE2",
        'Pag': "N",
        'Code': "TYU1235",
    },
    {
        'DataAge': "2020-01-7 ",
        'DataCad': "2019-01-07T13:16:00.567",
        'HoraAge': "15:15",
        'Field1': "Uppercase",
        'Pag': "N",
        'Code': "JJJ1212",
    }
]

array1.forEach(function (element1) {
    var found = array2.find(function (element2) {
        return element2.Code == element1.Code;
    });
    if (found) {
        var requiredElement = Object.keys(element1).length > Object.keys(found).length ? element1 : found;
        finalArray.push(requiredElement);
    }
});
console.log(finalArray);

Upvotes: 0

Related Questions