Yusnee Yusnee - 7 months ago 47
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?


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

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