Javascript filter with an array -


here's code:

data = [   {     company: "company name",     type: "mining",     location: "middle east"   },   {     company: "company name 2",     type: "distribution outlet",     location: "europe"   },   ... ]; myfilter = [   "reprocessing plant",   "mining",   "processing plant/mine",   "distribution outlet" ]; var myresults = []; results = data.filter(function(el) {   if (myfilter.indexof(el.type) != -1 && myfilter.indexof(el.location) != -1) {     return el;   } });  console.log(results); 

(original: https://jsfiddle.net/rpsb8buh/4/)

requirement :

i have data array , want filter them based on following array filters, working fine if use type && location this

myfilter = ['reprocessing plant','mining','processing plant/mine','distribution outlet','europe']; 

but if pass filter without location wont work

myfilter = ['reprocessing plant','mining','processing plant/mine','distribution outlet']; 

updated fiddle

because && return both conditions true. better use array#includes()

updated

you should validate array length after filtering.then empty assign data array or filter type matching array wish

var data = [ { "company": "company name", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "9714 883 7571", "email": "gma@emirates.net.ae", "website": "www.emirates.net.au", "markercolor": 'pink', "type": "mining", "location": "middle east", "latitude": 25.856407, "longitude": 17.247047 }, { "company": "company name 2", "address": "lorem ipsum dolor sit amet, vel no congue quaeque eruditi", "telphone": "", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'brown', "type": "distribution outlet", "location": "europe", "latitude": 45.526054, "longitude": 3.892456 }, { "company": "company name 3", "address": "lorem ipsum dolor sit amet, vel no congue quaeque eruditi", "telphone": "22115 48558", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'yellow', "type": "reprocessing plant", "location": "middle east", "latitude": 20.526054, "longitude": 1.892456 }, { "company": "company name 4", "address": "", "telphone": "221 981 558", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'light-pink', "type": "processing plant/mine", "location": "europe", "latitude": 48.526054, "longitude": 26.892456 }, { "company": "company name 5", "address": "", "telphone": "111 922 252", "email": "gma@emirates.net.ae", "website": "www.example.com", "markercolor": 'yellow', "type": "reprocessing plant", "location": "middle east", "latitude": 32.526054, "longitude": 36.892456 }, { "company": "company name 6", "address": "", "telphone": "4361 922 252", "email": "gma@emirates.net.ae", "website": "www.example.com", "markercolor": 'yellow', "type": "reprocessing plant", "location": "americas", "latitude": -32.526054, "longitude": -66.892456 }, { "company": "company name 7", "address": "", "telphone": "4361 332 111", "email": "gma@emirates.net.ae", "website": "www.ssaww.com", "markercolor": 'brown', "type": "distribution outlet", "location": "middle east", "latitude": -32.526054, "longitude": 26.892456 }, { "company": "company name 8", "address": "", "telphone": "221 981 558", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'light-pink', "type": "processing plant/mine", "location": "asia pacific", "latitude": 18.526054, "longitude": 76.892456 }, { "company": "company name 9", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "9714 883 7571", "email": "gma@emirates.net.ae", "website": "www.emirates.net.au", "markercolor": 'pink', "type": "mining", "location": "middle east", "latitude": -5.856407, "longitude": 20.247047 }, { "company": "company name 10", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "", "email": "gma@emirates.net.ae", "website": "www.emirates.net.au", "markercolor": 'brown', "type": "distribution outlet", "location": "asia pacific", "latitude": -25.856407, "longitude": 120.247047 }, { "company": "company name 11", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "", "email": "gma@emirates.net.ae", "website": "www.emirates.net.au", "markercolor": 'yellow', "type": "reprocessing plant", "location": "americas", "latitude": 55.856407, "longitude": -120.247047 }, { "company": "company name 12", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'pink', "type": "mining", "location": "americas", "latitude": 25.856407, "longitude": -100.247047 }, { "company": "company name 13", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'pink', "type": "mining", "location": "asia pacific", "latitude": 4.062187, "longitude": 101.496645 }, { "company": "company name 14", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "2233 65448 21", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'yellow', "type": "reprocessing plant", "location": "asia pacific", "latitude": 22.556300, "longitude": 113.916657 }, { "company": "company name 15", "address": "lorem ipsum dolor sit amet quaeque eruditi", "telphone": "2233 65448 21", "email": "gma@emirates.net.ae", "website": "www.example.com", "markercolor": 'light-pink', "type": "processing plant/mine", "location": "americas", "latitude": 25.826347, "longitude": -80.207989 }, { "company": "company name 16", "address": "lorem ipsum dolor sit amet, vel no congue quaeque eruditi", "telphone": "", "email": "gma@emirates.net.ae", "website": "", "markercolor": 'yellow', "type": "reprocessing plant", "location": "europe", "latitude": 39.526054, "longitude": -3.892456 } ];    var  myfilter = ['reprocessing plant','mining','processing plant/mine','distribution outlet','europe'];  var myresults= [];   var results = data.filter(function(el){	               return myfilter.includes(el.type) &&  myfilter.includes(el.location)      })  		  		if(results.length ==0 ){ //if array empty  		results = data.filter(function(el){	               return myfilter.includes(el.type);//set  matched type  		})  		//or if need   		//assign  		//results = data  		}    console.log(results)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -