1

const feature = [
    {
      attributes: {
        data: {
          rootType: { type: "string", value: "Alpha" },
          rootAge: { type: "integer", value: "10" },
          baySize: { type: "integer", value: "18" },
          totalBays: { type: "integer", value: "13" }
        }
      }
    },
    {
      attributes: {
        data: {
          rootType: { type: "string", value: "Brown" },
          rootAge: { type: "integer", value: "20" },
          baySize: { type: "integer", value: "180" },
          totalBays: { type: "integer", value: "30" }
        }
      }
    },
    {
      attributes: {
        data: {
          rootType: { type: "string", value: "Alpha" },
          rootAge: { type: "integer", value: "50" },
          baySize: { type: "integer", value: "80" },
          totalBays: { type: "integer", value: "25" }
        }
      }
    }
  ];
  const allData = feature.map((item) => {
    const { data } = item.attributes;
    return data
  });
console.log(allData);

const expectedResult = [
  {
    rootType: { type: "string", value: "Alpha" },
    rootAge: { type: "integer", value: "10" },
    baySize: { type: "integer", value: "18" },
    totalBays: { type: "integer", value: "13" }
  },
  {
    rootType: { type: "string", value: "Brown" },
    rootAge: { type: "integer", value: "20" },
    baySize: { type: "integer", value: "180" },
    totalBays: { type: "integer", value: "30" }
  },
  {
    rootType: { type: "string", value: "Alpha" },
    rootAge: { type: "integer", value: "50" },
    baySize: { type: "integer", value: "80" },
    totalBays: { type: "integer", value: "25" }
  }
]

console.log(expectedResult)

I have array of object which I get from API.

enter image description here

I need a way to add these objects into array as below.

enter image description here

I have tried mapping over the original array and push these objects to new variable but I can only ever get the first one.

what is the best way to achieve the desired result.

looking forward to your response. thanks

Heera
  • 180
  • 12

1 Answers1

0

This script will push each object into the created array. You could create the expected Array first. You should Push it right after you create the array.

var expectedResult = [];

const dataOne = {
        rootType: { type: "string", value: "Alpha" },
        rootAge: { type: "integer", value: "10" },
        baySize: { type: "integer", value: "18" },
        totalBays: { type: "integer", value: "13" }
      };
expectedResult.push(dataOne);
      const dataTwo = {
        rootType: { type: "string", value: "Brown" },
        rootAge: { type: "integer", value: "20" },
        baySize: { type: "integer", value: "180" },
        totalBays: { type: "integer", value: "30" }
      };
expectedResult.push(dataTwo);
      const dataThree = {
        rootType: { type: "string", value: "Alpha" },
        rootAge: { type: "integer", value: "50" },
        baySize: { type: "integer", value: "80" },
        totalBays: { type: "integer", value: "25" }
      };

expectedResult.push(dataThree);
GerryMM88
  • 211
  • 2
  • 13
  • 1
    Just an opinion but the whole section on `const` is just noise unrelated to OP's problem – charlietfl Sep 15 '20 at 05:42
  • 1
    I don't really get why half the answer is about `const` and even then some of the bullet point are...odd, to say the least: "*Primitive value.*" ??? "*Cannot be Hoisted.*" [it *is* hoisted](https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-hoisted) – VLAZ Sep 15 '20 at 05:53