Yusnee Yusnee - 1 month ago 11
Javascript Question

add state on country array dynamically in js

var countries = ['India', 'China', 'Malaysia'];

var states = ['Penang', 'Johor', 'KL']; // for Malaysia


How can I add the states on related country array? Is that mean country array become 2 dimensional?

Answer

You could use an object to describe what a country is, instead of a name:

const countries = [{...}, {...}, {
    name: 'Malaysia',
    states: ['Penang', 'Johor', 'KL']
}, {...}];
Comments