简体   繁体   中英

how could i return the value inside of a foreach javascript

I have a function that needs to return the id of a country.

My list is an array of objects :

{
    id: 2,
    code: "AL",
    name: "Albania"
}...

Here is my function to get the id needed from country

getCountryID(countryName) {
            return country_list_with_id.forEach(res => {
                if (res.name.toLowerCase() === countryName) {
    console.log("the id is",res.id)// the log is 143
                    let id = res.id;
                    return id;
                }
            });
        }
    console.log(array.getCountryID("USA"))//undefined

so how could I get the id?

You can't. forEach is not intended to return anything, but you can use another function to get the id from the array.

Using find will return you an object which is satisfies your condition.

getCountry(countryName) {
    return country_list_with_id.find(item => item.name.toLowerCase() === countryName);
}

This will return the country object and from that object you can inject the id. If nothing was found, undefined is returned. So you need to check that object first, then try to access its properties.

const country = array.getCountry("USA");

console.log(country && country.id);

You can filter your array of countries to get the country you want, and return result. countries[0] might be undefined so use an if statement from my example or ternary operator from @void 's example Here's the snippet:

 const countries = [{ id: 2, code: "AL", name: "Albania" }, { id: 3, code: "DZ", name: "Algeria" }, { id: 4, code: "DS", name: "American Samoa" }]; function getCountryId(code) { const country = countries.filter(country => country.code === code); if(country.length > 0) { return country[0].name; } else { return "No such country."; } } console.log(getCountryId("DZ")); console.log(getCountryId("USA")); 

You can use Array.prototype.filter in order to filter out the country by name, and then return the id of the first/last item.

 const list = [{ id: 2, code: "AL", name: "Albania" }, { id: 3, code: "DZ", name: "Algeria" }, { id: 4, code: "DS", name: "American Samoa" }]; function getCountryId(name) { return (list.filter((country) => country.name === name)[0] || {}).id; } console.log(getCountryId('Algeria')); console.log(getCountryId('NoneExistingCountry')); 

Use need to use .filter here. This will return the items from array matching specific condition

  var data = [{ id: 2, code: "AL", name: "Albania" }, { id: 3, code: "DZ", name: "Algeria" }, { id: 4, code: "DS", name: "American Samoa" }] Array.prototype.getCountryID = function(code){ var output = this.filter(el => el.code === code); return output.length > 0 ? output[0].id : "Not Found"; } console.log(data.getCountryID("DS")); console.log(data.getCountryID("something else")); 

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