简体   繁体   English

在数组内展平数组

[英]Flatten array within array

I'm getting data from a geojson file and I want to flatten an array within an array to be able to show this data within a material table. 我正在从geojson文件获取数据,我想展平数组中的数组以能够在材料表中显示此数据。

I have the following code: 我有以下代码:

          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;
    }));

My logger output looks like that: 我的记录器输出如下所示:

[{"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}]

But I want something like that: 但是我想要这样的东西:

[{"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}]

I'm close, but I can't find the solution. 我已经接近了,但是找不到解决方案。

Do you have any idea? 你有什么主意吗?

Instead of pushing it to array , add property directly to the object 无需将其推送到array ,而是直接将属性添加到object

// 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);
});

Well technically it's not an array of arrays, but rather a collection of objects that you want to merge : 从技术上讲,它不是数组的数组,而是要合并的对象的集合:

 const data = [ { id: 1 }, { lat: 10 }, { lng: 20 }, ]; console.log(data.reduce((p, n) => ({...p, ...n}), {})); 

Just in case you really want a flattening function, here it goes : 万一您真的想要展平的功能,可以这样:

 const data = [ [{ id: 1 }], [{ lat: 10 }], [{ lng: 20 }], ]; console.log(Array.prototype.concat.apply([], data)); 

Mixing the two ? 混合两者?

 const data = [ [{ id: 1 }], [{ lat: 10 }], [{ lng: 20 }], ]; console.log((Array.prototype.concat.apply([], data)).reduce((p, n) => ({...p, ...n}), {})); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM