gpt4 book ai didi

javascript - 将数组内的数组展平

转载 作者:行者123 更新时间:2023-11-28 17:03:52 26 4
gpt4 key购买 nike

我正在从 geojson 文件获取数据,并且想要展平数组中的数组,以便能够在 Material 表中显示此数据。

我有以下代码:

          const geoListData: Array<any> = [];
const features = geoData.features; // feature array
const latIdx = 0;
const lonIdx = 1;

// iterate over each feature
features.forEach((feature: any) => {
const geoListArray: any = [];

// build up GeoListEntry
const geoListEntry: GeoListEntry = {
name: feature.properties.name,
category: feature.properties.category,
lat: feature.geometry.coordinates[latIdx],
lon: feature.geometry.coordinates[lonIdx],
prio: feature.properties.prio
};

geoListArray.push(geoListEntry);

// get values from geojson
feature.properties.values.forEach((element: any) => {
const valuesEntry: any = {
[element.name]: element.value
};
geoListArray.push(valuesEntry);
});

this.logger.debug(`geoListArray: ${JSON.stringify(geoListArray)}`);

geoListData.push(geoListArray);
});

return geoListData;
}));

我的记录器输出如下所示:

[{"name":"90","category":"Arc 12 month","lat":7.613333333,"lon":47.555555,"prio":0},{"bearing":12345},{"intensity_mean":0},{"intensity_min":0},{"intensity_max":0}]

但我想要这样的东西:

[{"name":"90","category":"Arc 12 month","lat":7.613333333,"lon":47.555555,"prio":0,"bearing":12345,"intensity_mean":0,"intensity_min":0,"intensity_max":0}]

我已经很接近了,但我找不到解决方案。

你有什么想法吗?

最佳答案

不要将其推送到数组,而是直接将属性添加到对象

// iterate over each feature
features.forEach((feature: any) => {
const geoListArray: any = [];

// build up GeoListEntry
const geoListEntry: GeoListEntry = {
name: feature.properties.name,
category: feature.properties.category,
lat: feature.geometry.coordinates[latIdx],
lon: feature.geometry.coordinates[lonIdx],
prio: feature.properties.prio
};

// get values from geojson
feature.properties.values.forEach((element: any) => {
geoListEntry[element.name] = element.value
});

geoListArray.push(geoListEntry);

this.logger.debug(`geoListArray: ${JSON.stringify(geoListArray)}`);

geoListData.push(geoListArray);
});

关于javascript - 将数组内的数组展平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56493716/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com