Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rerurn filtered array which is not equal to another array list

I’ve a api with list of car.

const getAsset = async () => dbApi('list_cars', ['', 100])
     .then(result => result.map(e => e.symbol));

here, getAsset returns the car list.

0: "BMW"
1: "HONDA"
2: "SCODA"
3: "STAR"
4: "PETH"
5: "TEST"
6: "TESLA"

In the below code block, I tried to return list excluding BMW. And it worked well.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 const getAsset = async () => dbApi('list_cars', ['', 100])
     .then(result => result.filter(e => e.symbol != 'BMW' ).map(a => a.symbol));

Now, I want to go forward and return the list of car excluding BMW , HONDA and TEST.

const expect = ['BMW','HONDA','TEST'];

expected result :

0: "SCODA"
1: "STAR"
2: "PETH"
3: "TESLA"

What changes should I make in the above code block?

>Solution :

You can do it by filter method, like this:

const cars = ["BMW","HONDA", "SCODA", "STAR", "PETH", "TEST", "TESLA"]
const except = ["BMW","HONDA","TEST"];

const filteredCars = cars.filter(car => !except.includes(car));
console.log(filteredCars)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading