0

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.

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?

3
  • 1
    That's not an array within an array - it's a series of objects within an array. And you want to combine them into a single object inside an array. Commented Jun 7, 2019 at 11:49
  • 2
    Can you post your initial data? Commented Jun 7, 2019 at 11:51
  • After @VLAZ clarification you should edit the question and make it clearer, specify what you really want since the desired output doesn't match the description of what you want. Commented Jun 7, 2019 at 11:51

1 Answer 1

3

Instead of pushing it to array, add property directly to the 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);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.