2

I have two arrays. I need to combine both of them and make a new array which has dayOfWeek 2, 3, 4, 5, 6. Which means priority for the dayOfWeek is in array1. Means need to keep dayOfWeek 3, 4, 5 from array1.

array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
]

array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

Output should be

finalArray = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

I tried this but it pushes the dayOfWeek from both the arrays. How can I filter them?

const finalArray = []
array1.map((a) => {
    array2.map((a2) => {
        if (a.dayOfWeek === a2.dayOfWeek) {
          finalArray.push(a)
        }
        if (a.dayOfWeek === a2.dayOfWeek) {
          finalArray.push(a2)
        }
    })
})

Thanks in advance!!!

Profer
  • 553
  • 8
  • 40
  • 81

6 Answers6

3

You could also simply filter the second array for the items missing form the first and then concatenate those to the first array without lodash:

const a1 = [ {dayOfWeek: 2, home1: "01:30"}, {dayOfWeek: 3, home1: "02:30"}, {dayOfWeek: 4, home1: "03:30"}, {dayOfWeek: 5, home1: "04:30"}, ]
const a2 = [ {dayOfWeek: 3, home1: "05:30"}, {dayOfWeek: 4, home1: "06:30"}, {dayOfWeek: 5, home1: "07:30"}, {dayOfWeek: 6, home1: "08:30"}, ]

const r = a1.concat(a2.filter(x => !a1.some(y => y.dayOfWeek == x.dayOfWeek)))

console.log(r)

This is done via Array.concat, Array.filter and Array.some

Akrion
  • 18,117
  • 1
  • 34
  • 54
2

You can also use Set and Array.filter

let array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
]

let array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

let s = new Set()
console.log([...array1, ...array2].filter(d => {
    let avail = s.has(d.dayOfWeek)
    !avail && s.add(d.dayOfWeek)
    return !avail
  }
) )
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22
2

Use lodash's _.unionBy(). The predominant array should be the 1st array passed to the function.

const array1 = [{"dayOfWeek":2,"home1":"01:30"},{"dayOfWeek":3,"home1":"02:30"},{"dayOfWeek":4,"home1":"03:30"},{"dayOfWeek":5,"home1":"04:30"}]
const array2 = [{"dayOfWeek":3,"home1":"05:30"},{"dayOfWeek":4,"home1":"06:30"},{"dayOfWeek":5,"home1":"07:30"},{"dayOfWeek":6,"home1":"08:30"}]

const result = _.unionBy(array1, array2, 'dayOfWeek')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

If you need to combine several properties to use as the union value, you can use:

_.unionBy(array1, array2, o => `${o.id}-${o.dayOfWeek}`)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

you can use array concatenation method and after that do filter.

var c = array1.concat(array2);
shaan26
  • 37
  • 7
1

Profer. You could use Map object to get unique elements

const array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
];

const array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
];

function getFinalArray(array, uniqueProperty) {
  return array
    .filter(value => value)
    .reduce(
      (arrayMap, item) => {
        return arrayMap.set(item[uniqueProperty], item)
      }, new Map()
    );
}

const result = Array.from(
  getFinalArray([...array2, ...array1], 'dayOfWeek').values()
);
Evgeniy Boytsov
  • 214
  • 2
  • 8
  • If you don't want the order to get mangled you could do: `.reduce( (arrayMap, item) => !arrayMap.get(item[uniqueProperty]) ? arrayMap.set(item[uniqueProperty], item) : arrayMap, new Map(),` pass in the arrays as [...array1,...array2]` – HMR Nov 23 '18 at 08:50
  • Thanks for the addition. it could be nice to **do check** this way `arrayMap.has(item[uniqueProperty])` instead – Evgeniy Boytsov Nov 23 '18 at 09:02
1

You can use a combination of Array#slice(), Array#forEach() and Array#some() methods:

  • Use .slice(0) to get all elements of array1 in finalArray.
  • Then use .forEach() to iterate over array2 and get all elements that doesn't exist in finalArray.
  • Use .some() to check if the iterated element (dayOfWeek) exist.

This is how should be your code:

var finalArray = array1.slice(0);
array2.forEach(function(a){
      if(!finalArray.some(e => e.dayOfWeek == a.dayOfWeek))
          finalArray.push(a);
});

Demo:

let array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
];

let array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
];

var finalArray = array1.slice(0);
array2.forEach(function(a){
      if(!finalArray.some(e => e.dayOfWeek == a.dayOfWeek))
          finalArray.push(a);
});

console.log(finalArray);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78