0

I have two arrays :

let channelList = [
  {
    "channelId": 1,
    "channelName": "SMS"
  },
  {
    "channelId": 2,
    "channelName": "EMAIL"
  },
  {
    "channelId": 3,
    "channelName": "ANDROID"
  },
  {
    "channelId": 4,
    "channelName": "IOS"
  }
]

and

let promoList = [
  {
    "id": 124,
    "channelType": "SMS"
  },
  {
    "id": 125,
    "channelType": "ANDROID"
  },
  {
    "id": 126,
    "channelType": "IOS"
  }
]

I want a new array remainingChannels which has

{
    "channelId": 2,
    "channelName": "EMAIL"
}

because it is not present in promoList, but it is there in channelList.

I am trying to get it using the filter operation like this:

for(let channel of this.promoDetailList) {
     remainingChannels = this.channelList.filter(e => {
         e.channelName == channel.channelType
     });
}

How to do this?

  • 1
    Does this answer your question? [How to get the difference between two arrays in JavaScript?](https://stackoverflow.com/questions/1187518/how-to-get-the-difference-between-two-arrays-in-javascript) – VLAZ Sep 28 '20 at 13:36
  • No, in this question there is array of characters. I have array of objects. – Nehal Jaisalmeria Sep 28 '20 at 13:37
  • 1
    Just compare them by `channelName` instead. The logic for the array difference is the same otherwise. – VLAZ Sep 28 '20 at 13:38
  • VLAZ first comment is correct, just simplify one of the arrays of objects to a string array then follow the answer of that question. – Aus Sep 28 '20 at 13:48

3 Answers3

4

You can make use of Array.filter and Array.some

let channelList = [{"channelId":1,"channelName":"SMS"},{"channelId":2,"channelName":"EMAIL"},{"channelId":3,"channelName":"ANDROID"},{"channelId":4,"channelName":"IOS"}];

let promoList = [{"id":124,"channelType":"SMS"},{"id":125,"channelType":"ANDROID"},{"id":126,"channelType":"IOS"}]

//Check if the `channelName` from `channels` is matching with any of the
//`channelType` from `promos` list, negate the result. 
const filterData = (channels, promos) => channels.filter(channel => !promos.some(promo => promo.channelType === channel.channelName))

console.log(filterData(channelList, promoList));

This can also be achieved using Array.filter and Array.find.

let channelList = [{"channelId":1,"channelName":"SMS"},{"channelId":2,"channelName":"EMAIL"},{"channelId":3,"channelName":"ANDROID"},{"channelId":4,"channelName":"IOS"}];
let promoList = [{"id":124,"channelType":"SMS"},{"id":125,"channelType":"ANDROID"},{"id":126,"channelType":"IOS"}]

const filterData = (channels, promos) => channels.filter(({ channelName }) => !promos.find(({ channelType }) => channelName === channelType));

console.log(filterData(channelList, promoList))
Nithish
  • 5,393
  • 2
  • 9
  • 24
1

You can do it using Array.filter.

let channelList = [
  {
    "channelId": 1,
    "channelName": "SMS"
  },
  {
    "channelId": 2,
    "channelName": "EMAIL"
  },
  {
    "channelId": 3,
    "channelName": "ANDROID"
  },
  {
    "channelId": 4,
    "channelName": "IOS"
  }
]

let promoList = [
  {
    "id": 124,
    "channelType": "SMS"
  },
  {
    "id": 125,
    "channelType": "ANDROID"
  },
  {
    "id": 126,
    "channelType": "IOS"
  }
]

const result = channelList.filter(({channelName}) => {
  return promoList.filter(({ channelType }) => channelType === channelName).length == 0;
});
console.log(result);
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
0

Extract the items of one of the arrays into a string array.

let channelList = [
  {
    "channelId": 1,
    "channelName": "SMS"
  },
  {
    "channelId": 2,
    "channelName": "EMAIL"
  },
  {
    "channelId": 3,
    "channelName": "ANDROID"
  },
  {
    "channelId": 4,
    "channelName": "IOS"
  }
]

let promoList = [
  {
    "id": 124,
    "channelType": "SMS"
  },
  {
    "id": 125,
    "channelType": "ANDROID"
  },
  {
    "id": 126,
    "channelType": "IOS"
  }
]

function cross(a, b) {
  const b_ = b.map(item => item.channelType)
  const res = []
  a.forEach(item => {if (!b_.includes(item.channelName)) res.push(item)})
  console.log(res)
}
cross(channelList, promoList)
Aus
  • 1,183
  • 11
  • 27