简体   繁体   中英

How to filter objects from one array from another array in JavaScript?

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?

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM