1

This is my array

var CountryList = [{ name: Canada, value: 1 },
    { name: USA, value: 2 }]

This is the operation I am using right now

var filterStr = nextProps.Country.value == "1" ? 'Canada' : 'USA';

Now I want to use the array CountryList in the above code using to check whether Canada or USA is selected. Is it done using map? If it's USA I want to populate States of USA in another Dropdown. If it's Canada Then States of Canada. SO according to the Selection from this array I want to populate the States. I need to check the selected country . ie, I want to get the country name. How to do that?

3 Answers 3

1
var states = {USA: [{name: 'A', value:1},..],Canada: []} 
var country = CountryList.find(country => country.value == nextProps.Country.value).name;
if (states[county]){
    this.setState({states: states[country], enabledStates: true});
}else{
    this.setState({states: [], enabledStates: false});
}

Show the states based on the flag enabledState.

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

1 Comment

is it 'nextProps.CountryList.value' because I am using the array CountryList
0

if you want an array of all countries that have the name USA for exemple, you can use filter like this :

CountryList.filter = country => country.value === 1 ;

or if want to check if USA for exemple exist in this array, you can use "find"

Comments

0

map is not the correct function to use over here. You should use either find or filter function. let country = CountryList.find((c) => (c.value === 1)) country = country && country.name

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.