35

I have an array activeIds of ids of services and there is another array servicesList which contains objects of services.

Example: -

activeIds = [202, 204]
serviceList = [{  
                "id":201,
                "title":"a"
               },
               {  
                "id":202,
                "title":"a"
               },
               {  
                "id":203,
                "title":"c"
               },
               {  
                "id":204,
                "title":"d"
               },
               {  
                "id":205,
                "title":"e"
               }];

I want all the services(obj) whose ids are not a part of the first array i.e., activeIds. From the above example code I want service obj of ids 201,203,205

Final output -

expectedArray = [{  
                "id":201,
                "title":"a"
               },
               {  
                "id":203,
                "title":"c"
               },
               {  
                "id":205,
                "title":"e"
               }];

Here is my attempt to code. But it is not correct at all. Please help-

    const activeIds = e; // [202, 204]
    const obj = [];
    this.serviceList.map((s: IService) => {
        activeIds.map((id: number) => {
            if (id !== s.id) {
                obj.push(s);
            }
        });
    });
1
  • If the id array is long i suggest that you convert it to an object lookup or a map to avoid linear searching through the array for each element. Otherwise using the array.filter function is the way to go like suggested in every answer below. Commented Feb 17, 2018 at 18:40

4 Answers 4

35

You can simply use array.filter with indexOf to check the matching element in the next array.

var arr = serviceList.filter(item => activeIds.indexOf(item.id) === -1);

DEMO

let activeIds = [202, 204]
let serviceList = [{  
                "id":201,
                "title":"a"
               },
               {  
                "id":202,
                "title":"a"
               },
               {  
                "id":203,
                "title":"c"
               },
               {  
                "id":204,
                "title":"d"
               },
               {  
                "id":205,
                "title":"e"
               }];


let  arr = serviceList.filter(function(item){
      return activeIds.indexOf(item.id) === -1;
    });
    
console.log(arr);

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

1 Comment

How is the performance of this if serviceList is really huge?
30

You can do this with filter and includes methods.

const activeIds = [202, 204]
const serviceList = [{"id":201,"title":"a"},{"id":202,"title":"a"},{"id":203,"title":"c"},{"id":204,"title":"d"},{"id":205,"title":"e"}]

const result = serviceList.filter(({id}) => !activeIds.includes(id));
console.log(result)

3 Comments

op uses arrow function, why should not include work?
initially he did not had that in question! or i might have missed :(
Nice, I needed the opposite one, but just leaving out the ! in front of activeIds was my solution. Take your upvote sir!
4

Use filter & indexOf method.indexOf will check if the current id is present in activeIds array

var activeIds = [202, 204]
var serviceList = [{
    "id": 201,
    "title": "a"
  },
  {
    "id": 202,
    "title": "a"
  },
  {
    "id": 203,
    "title": "c"
  },
  {
    "id": 204,
    "title": "d"
  },
  {
    "id": 205,
    "title": "e"
  }
];

var filteredArray = serviceList.filter(function(item) {
  return activeIds.indexOf(item.id) === -1

});

console.log(filteredArray)

2 Comments

is not the same posted above
I have same doubt.Seems we posted almost at same time
2

you can combine indexOf to check if the current id is on the active array and filter the array.

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.