0

I define a object like this using Javacript:

const city = {
  "北京市": ["北京市"],
  "天津市": ["天津市"],
  "上海市": ["上海市"],
  "重庆市": ["重庆市"],
  "香港": ["香港"],
  "澳门": ["澳门"],
  "台湾省": ["台湾省"]
};

now I want to foreach the city key with other strings I defined like this:

const region = [
  "北京市",
  "天津市",
];

but I did not know how to foreach the key of city, what I want to do may look like this:

var options = region.map(function (item){
    
     children = city.map(function (itemc){
       if(itemc.key === item){
         return itemc.value
       }
     });
      return ({
        label: item,
        children: children
      });
    });

but the city did not contains keys or index for me to compare, what should I do to make it work?

Dolphin
  • 13,739
  • 26
  • 110
  • 272
  • 1
    Use `city[item]`. (`var options = region.map(item => ({label: item, children: city[item]}));`) – Ivar Nov 30 '21 at 10:43

0 Answers0