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?

4
  • 1
    Does this answer your question? How to get the difference between two arrays in JavaScript? Commented Sep 28, 2020 at 13:36
  • No, in this question there is array of characters. I have array of objects. Commented Sep 28, 2020 at 13:37
  • 1
    Just compare them by channelName instead. The logic for the array difference is the same otherwise. Commented Sep 28, 2020 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. Commented Sep 28, 2020 at 13:48

3 Answers 3

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

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

2 Comments

Is there an alternative to using !this.promoDetailList.some?
Can also be achieved using Array.find, or Array.filter or Array.findIndex. I have updated my answer using Array.find
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);

2 Comments

Why are we using ({channelName}) like this here?
To compare the channelName value with promoList. So to check validation.
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.