js 将一个数组中的部分字段,添加到另外一个数组中

原来的data数组

js 将一个数组中的部分字段,添加到另外一个数组中_第1张图片

{"data":[{"id":"340129","parentId":"340100","name":"高新区","shortName":"高新技术产业开发区","sort":1,"type":3,"lng":"117.135790","lat":"31.831280","pinyin":"gaoxinjishuchanyekaifaqu"},{"id":"340128","parentId":"340100","name":"经开区","shortName":"经济技术开发区","sort":1,"type":3,"lng":"117.248120","lat":"31.784890","pinyin":"jingjijishukaifaqu"},{"id":"340127","parentId":"340100","name":"滨湖新区","shortName":"滨湖新区","sort":1,"type":3,"lng":"117.920520","lat":"31.647600","pinyin":"binhuxinqu"},{"id":"340126","parentId":"340100","name":"新站区","shortName":"新站区","sort":1,"type":3,"lng":"117.338630","lat":"31.906640","pinyin":"xinzhanqu"},{"id":"340125","parentId":"340100","name":"巢湖市","shortName":"巢湖市","sort":1,"type":3,"lng":"117.889370","lat":"31.623290","pinyin":"chaohushi"},{"id":"340124","parentId":"340100","name":"庐江县","shortName":"庐江县","sort":1,"type":3,"lng":"117.287360","lat":"31.255670","pinyin":"lujiangxian"},{"id":"340123","parentId":"340100","name":"肥西县","shortName":"肥西县","sort":1,"type":3,"lng":"117.182830","lat":"31.706890","pinyin":"feixixian"},{"id":"340122","parentId":"340100","name":"肥东县","shortName":"肥东县","sort":1,"type":3,"lng":"117.498612","lat":"31.898002","pinyin":"feidongxian"},{"id":"340121","parentId":"340100","name":"长丰县","shortName":"长丰县","sort":1,"type":3,"lng":"117.173072","lat":"32.483919","pinyin":"zhangfengxian"},{"id":"340111","parentId":"340100","name":"包河区","shortName":"包河","sort":1,"type":3,"lng":"117.285751","lat":"31.829559","pinyin":"baohe"},{"id":"340103","parentId":"340100","name":"庐阳区","shortName":"庐阳","sort":7,"type":3,"lng":"117.283775","lat":"31.869011","pinyin":"luyang"},{"id":"340104","parentId":"340100","name":"蜀山区","shortName":"蜀山","sort":8,"type":3,"lng":"117.26207","lat":"31.855867","pinyin":"shushan"},{"id":"340102","parentId":"340100","name":"瑶海区","shortName":"瑶海","sort":9,"type":3,"lng":"117.315361","lat":"31.86961","pinyin":"yaohai"}]}

截取部分字段

  var area = []
          data.forEach((item) => {
            area.push({ areaCode: item.id, areaName: item.name })
          })

生成的新数组

{
     "area":[{
     "areaCode":"340129","areaName":"高新区"},{
     "areaCode":"340128","areaName":"经开区"},{
     "areaCode":"340127","areaName":"滨湖新区"},{
     "areaCode":"340126","areaName":"新站区"},{
     "areaCode":"340125","areaName":"巢湖市"},{
     "areaCode":"340124","areaName":"庐江县"},{
     "areaCode":"340123","areaName":"肥西县"},{
     "areaCode":"340122","areaName":"肥东县"},{
     "areaCode":"340121","areaName":"长丰县"},{
     "areaCode":"340111","areaName":"包河区"},{
     "areaCode":"340103","areaName":"庐阳区"},{
     "areaCode":"340104","areaName":"蜀山区"},{
     "areaCode":"340102","areaName":"瑶海区"}]}

在这里插入图片描述

你可能感兴趣的:(javaScript)