hello我想转换一个对象在另一个指定信息的第一个



我需要一个指定的对象来做一个响应式下拉与jsp,其中部门活动市政,我这样做的方式是javascript与json预格式化的对象但我需要来自数据库

但是我从db得到的对象是不同的,你能帮我吗?只需要在对象B中变换对象A

:

{
"coddpt":"01",
"namedpt":"AHUACHAPAN ",
"codmun":"01",
"namemun":"AHUACHAPAN "
},
{
"coddpt":"02",
"namedpt":"SANTA ANA ",
"codmun":"01",
"namemun":"SANTA ANA "
},
{
"coddpt":"03",
"namedpt":"SONSONATE ",
"codmun":"01",
"namemun":"SONSONATE "
},
{
"coddpt":"04",
"namedpt":"CHALATENANGO ",
"codmun":"01",
"namemun":"CHALATENANGO "
},
{
"coddpt":"05",
"namedpt":"LA LIBERTAD ",
"codmun":"01",
"namemun":"LA LIBERTAD "
},
{
"coddpt":"06",
"namedpt":"SAN SALVADOR ",
"codmun":"01",
"namemun":"SAN SALVADOR "
},
{
"coddpt":"08",
"namedpt":"LA PAZ ",
"codmun":"01",
"namemun":"LA PAZ "
},
{
"coddpt":"09",
"namedpt":"CABAÑAS ",
"codmun":"01",
"namemun":"CABAÑAS "
},
{
"coddpt":"10",
"namedpt":"SAN VICENTE ",
"codmun":"01",
"namemun":"SAN VICENTE "
},
{
"coddpt":"11",
"namedpt":"USULUTAN ",
"codmun":"01",
"namemun":"USULUTAN "
},
{
"coddpt":"12",
"namedpt":"SAN MIGUEL ",
"codmun":"01",
"namemun":"SAN MIGUEL "
},
{
"coddpt":"13",
"namedpt":"MORAZAN ",
"codmun":"01",
"namemun":"MORAZAN "
},
{
"coddpt":"01",
"namedpt":"AHUACHAPAN ",
"codmun":"02",
"namemun":"CANDELARIA DE LA FRONTERA "
},
{
"coddpt":"02",
"namedpt":"SANTA ANA ",
"codmun":"02",
"namemun":"COATEPEQUE "
},
{
"coddpt":"03",
"namedpt":"SONSONATE ",
"codmun":"02",
"namemun":"CHALCHUAPA "
},
{
"coddpt":"04",
"namedpt":"CHALATENANGO ",
"codmun":"02",
"namemun":"EL CONGO "
},
{
"coddpt":"05",
"namedpt":"LA LIBERTAD ",
"codmun":"02",
"namemun":"EL PORVENIR "
},
{
"coddpt":"06",
"namedpt":"SAN SALVADOR ",
"codmun":"02",
"namemun":"MASAHUAT "
},
{
"coddpt":"07",
"namedpt":"CUSCATLAN ",
"codmun":"02",
"namemun":"METAPAN "
},
{
"coddpt":"08",
"namedpt":"LA PAZ ",
"codmun":"02",
"namemun":"SAN ANTONIO PAJONAL "
},
{
"coddpt":"09",
"namedpt":"CABAÑAS ",
"codmun":"02",
"namemun":"SAN SEBASTIAN SALITRILLO "
},
{
"coddpt":"10",
"namedpt":"SAN VICENTE ",
"codmun":"02",
"namemun":"SANTA ANA "
},
{
"coddpt":"11",
"namedpt":"USULUTAN ",
"codmun":"02",
"namemun":"STAROSA GUACHI "
},
{
"coddpt":"12",
"namedpt":"SAN MIGUEL ",
"codmun":"02",
"namemun":"STGO D LA FRONT "
},
{
"coddpt":"13",
"namedpt":"MORAZAN ",
"codmun":"02",
"namemun":"TEXISTEPEQUE "
},
{
"coddpt":"01",
"namedpt":"AHUACHAPAN ",
"codmun":"03",
"namemun":"ACAJUTLA "
},
{
"coddpt":"02",
"namedpt":"SANTA ANA ",
"codmun":"03",
"namemun":"ARMENIA "
},
{
"coddpt":"03",
"namedpt":"SONSONATE ",
"codmun":"03",
"namemun":"CALUCO "
},
{
"coddpt":"04",
"namedpt":"CHALATENANGO ",
"codmun":"03",
"namemun":"CUISNAHUAT "
},
{
"coddpt":"05",
"namedpt":"LA LIBERTAD ",
"codmun":"06",
"namemun":"COATEPEQUE "
}
]

{
"AHUACHAPAN":["AHUACHAPAN","CANDELARIA DE LA FRONTERA","ACAJUTLA"],
"SANTA ANA": ["SANTA ANA","COATEPEQUE","ARMENIA"],
"SONSONATE": ["SONSONATE","CHALCHUAPA","CALUCO"],
"CHALATENANGO":["CHALATENANGO","EL CONGO","CUISNAHUAT"],
"LA LIBERTAD":["LA LIBERTAD","EL PORVENIR","COATEPEQUE"],
"SAN SALVADOR":["SAN SALVADOR","MASAHUAT"],
"CUSCATLAN":["METAPAN"],
"LA PAZ":["LA PAZ","SAN ANTONIO PAJONAL"],
"CABAÑAS":["CABAÑAS","SAN SEBASTIAN SALITRILLO"],
"SAN VICENTE":["SAN VICENTE","SANTA ANA"],
"USULUTAN":["USULUTAN","STAROSA GUACHI"],
"SAN MIGUEL":["SAN MIGUEL","STGO D LA FRONT"],
"MORAZAN":["MORAZAN","TEXISTEPEQUE"]
}
var B = {};
A.forEach(x => {
B[x.namedpt] ? B[x.namedpt].push(x.namemun) : B[x.namedpt] = Array(x.namemun);
})
  1. 初始化对象B为空对象
  2. 遍历所有Array元素
  3. 第一个namedpt将不在对象b中,因此,新数组将赋值给当前的namedpt。
  4. 下一次,current namedpt将有一个数组。所以,这次新元素会推送到它。

相关内容

  • 没有找到相关文章

最新更新