Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从阵列本身重新构造阵列?_Javascript_Arrays_Json_Angular_Typescript - Fatal编程技术网

Javascript 如何从阵列本身重新构造阵列?

Javascript 如何从阵列本身重新构造阵列?,javascript,arrays,json,angular,typescript,Javascript,Arrays,Json,Angular,Typescript,我的阵列是: 我希望它是这样的- 我写的函数如下:weher AirportCity相同且子优先级不为空 const customAirports = []; myArray.map((val, i, arr) => { let pushedInSubAirport = false; for (let v of customAirports) { if (v.AirportCity ===

我的阵列是:

我希望它是这样的-

我写的函数如下:weher AirportCity相同且子优先级不为空

const customAirports = [];
        myArray.map((val, i, arr) => {
            let pushedInSubAirport = false;
            for (let v of customAirports) {
                if (v.AirportCity === (val.SubPriority ?
                    val.AirportCity : undefined)
                ) {
                    if (!v['SubAirport'])
                        Object.assign(v, { SubAirport: [] })
                    v['SubAirport'].push(val);
                    pushedInSubAirport = true;
                    break;
                }
            }
            if (!pushedInSubAirport)
                customAirports.push(val)
        });
        myArray = customAirports;

如果不合适,请向我建议解决方案。

请注意数据一致性,例如
null
/
0

.map
返回另一个数组,如果不应该返回,最好使用
.forEach

避免在迭代时修改结构

const数据=[{
“Id”:1891年,
“机场代码”:“配音”,
“机场名称”:“都柏林机场”,
“机场城”:“都柏林”,
"优先":一,,
“次优先级”:0
}, {
“Id”:1921年,
“机场代码”:“DXB”,
“机场名称”:“迪拜国际机场”,
“机场城”:“迪拜”,
"优先":一,,
“次优先级”:0
}, {
“Id”:1680,
“机场代码”:“DBN”,
“机场名称”:“都柏林市机场”,
“机场城”:“都柏林”,
"优先":二,,
“次优先级”:1
}, {
“Id”:8973,
“机场代码”:“XNB”,
“机场名称”:“阿拉伯联合酋长国-公共汽车站”,
“机场城”:“迪拜”,
"优先":二,,
“次优先级”:1
}, {
“Id”:6062,
“机场代码”:“PSK”,
“机场名称”:“新河谷机场”,
“机场城”:“都柏林”,
“优先”:3,
“次优先级”:1
}, {
“Id”:1681,
“机场代码”:“DBO”,
“机场名称”:“Dubbo机场”,
“机场城”:“Dubbo”,
“优先权”:99,
“子优先级”:0//null-不一致!!!
}];
常量分组=组(数据);
window.result.innerHTML=JSON.stringify(分组,空,2);
功能组(数据){
const[main,subs]=数据。reduce(([main,subs],item)=>{
if(item.SubPriority==0){
返回[main.concat(项目),subs];
}
const subList=subs.get(item.AirportCity)| |[];
子集合(item.AirportCity,subList.concat(item));
返回[main,subs];
},[],新映射());
返回main.map((item)=>Object.assign(item,{SubAirport:subs.get(item.AirportCity)});
}
另一种方法

this.airports.sort((a,b)=>a.Priority-b.Priority) //sort by priority
    this.airports.forEach((x,index)=>{
      const airport=this.airports.find( //try find one more on top
          (a,index2)=>a.AirportCity==x.AirportCity && index>index2)
      if (airport) //if exist
      {
        if (!airport.SubAirport) //if has not SubAirport
            airport.SubAirport=[{...x}]  //create an array with one element
        else
          airport.SubAirport.push({...x}) //add to array

        x.remove=true; //mark as "removable"
      }

    })
    this.airports=this.airports.filter(x=>!x.remove) //<--remove the removable
this.airports.sort((a,b)=>a.Priority-b.Priority)//按优先级排序
这个.airports.forEach((x,索引)=>{
const airport=this.airports.find(//尝试在顶部再查找一个
(a,index2)=>a.AirportCity==x.AirportCity&&index>index2)
if(机场)//如果存在
{
if(!airport.SubAirport)//if没有SubAirport
airport.SubAirport=[{…x}]//使用一个元素创建一个数组
其他的
airport.SubAirport.push({…x})//添加到数组
x、 remove=true;//标记为“removable”
}
})

this.airports=this.airports.filter(x=>!x.remove)//转换的规则是什么?简单地说,为什么
[{…x}]
,而不是
[x]
推送(x)
?嵌套迭代`this.airports.forEach`和
airport=this.airports.find
内部可能不是很好的解决方案。@Kyr,spread操作符用于避免在添加的属性中添加属性“remove”(类似于ObjectAsign)。关于这个发现,我没有发现任何问题。啊,我明白了,传播
x
是有意义的,因为
x
变异为
。删除
。在数组中使用
.find
遍历同一数组不是问题,它只是O(n2)。
this.airports.sort((a,b)=>a.Priority-b.Priority) //sort by priority
    this.airports.forEach((x,index)=>{
      const airport=this.airports.find( //try find one more on top
          (a,index2)=>a.AirportCity==x.AirportCity && index>index2)
      if (airport) //if exist
      {
        if (!airport.SubAirport) //if has not SubAirport
            airport.SubAirport=[{...x}]  //create an array with one element
        else
          airport.SubAirport.push({...x}) //add to array

        x.remove=true; //mark as "removable"
      }

    })
    this.airports=this.airports.filter(x=>!x.remove) //<--remove the removable