简体   繁体   中英

Return filtered array in javascript

I though this was going to be straightforward. I have a dataset containing (foreach country) name, imfcode, lat,lon. I want to pass a code to a function that will filter the dataset returning the info that correspond to the code passed to it. So if I pass 512 I should get information relating to Afganistan. here my function, would someone mind telling me what I'm doing wring

function loadTrade(imfCode) {
  console.log ("Country code= ",imfCode)
  var sourceCountry=dataset.filter function(el){
    return el.imfcode===imfCode
  }
  console.log ("Source country= ",sourceCountry)
}

I keep getting an unexpected token and just can't see it. Many thanks

You missed the Parenthesis after dataset.filter

Change it to :

function loadTrade(imfCode) {
  console.log ("Country code= ",imfCode)
  var sourceCountry=dataset.filter(function(el){
    return el.imfcode===imfCode;
  });
  console.log ("Source country= ",sourceCountry);
}

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