0

My current array of objects looks like,

const data = [
  {
    "Type": "Location",
    "Name": "Water",
  },
  {
    "Type": "Location",
    "Coordinates": [
      {
        "Lat": 57.94182777439993,
        "Long": 79.50404114815193
      },
      {
        "Lat": 31.209277877460135,
        "Long": 78.80122177677728
      },
      {
        "Lat": 31.35950051982242,
        "Long": 105.15694820332524
      },
      {
        "Lat": 58.17432360099434,
        "Long": 105.42050546759074
      }
    ],
    "Name": "Water",
  },
  {
    "Type": "Location",
    "Coordinates": [
      {
        "Lat": 58.72972797827911,
        "Long": 76.90570777266291
      },
      {
        "Lat": 29.54717721331581,
        "Long": 76.37859324413196
      },
      {
        "Lat": 30.460511875130663,
        "Long": 105.19418747049103
      },
      {
        "Lat": 59.71902258556691,
        "Long": 106.7755310560839
      }
    ],
    "Name": "Water",
  }
];

Which need to be converted into this format,

[
    {
        "name": "Water",
        "coords": [
            [57.94182777439993, 79.50404114815193],
            [31.209277877460135, 78.80122177677728],
            [31.35950051982242, 105.15694820332524],
            [58.17432360099434, 105.42050546759074]
        ]
    },
    {
        "name": "Water",
        "coords": [
            [58.72972797827911, 76.90570777266291],
            [29.54717721331581, 76.37859324413196],
            [30.460511875130663, 105.19418747049103],
            [59.71902258556691, 106.7755310560839]
        ]
    }
]

I tried,

const output = data.reduce((accumulator, curr) => {
  if(curr.Coordinates) {
    const data = curr.Coordinates.map(({Lat, Long}) => [Lat, Long]);
    accumulator.push(data)
  };
  return accumulator;
}, []);

But, the code above stored my coords to a array of list.

5
  • 1
    Your desired output isn't a valid format. Those objects needs to have some sort of key Commented Sep 3, 2021 at 10:45
  • Your input is also not consistent. The last set has no name Commented Sep 3, 2021 at 10:52
  • There is no name property in the last entry. Commented Sep 3, 2021 at 10:55
  • @trincot Question Edited Commented Sep 3, 2021 at 10:59
  • 1
    I edited it more, as you didn't fix the output format. Commented Sep 3, 2021 at 11:00

2 Answers 2

2

In the reduce callback, just wrap the mapped coordinates in an object literal that also includes the name property:

const data = [{"Type": "Location","Name": "Water",},{"Type": "Location","Coordinates": [{"Lat": 57.94182777439993,"Long": 79.50404114815193},{"Lat": 31.209277877460135,"Long": 78.80122177677728},{"Lat": 31.35950051982242,"Long": 105.15694820332524},{"Lat": 58.17432360099434,"Long": 105.42050546759074}],"Name": "Water",},{"Type": "Location","Coordinates": [{"Lat": 58.72972797827911,"Long": 76.90570777266291},{"Lat": 29.54717721331581,"Long": 76.37859324413196},{"Lat": 30.460511875130663,"Long": 105.19418747049103},{"Lat": 59.71902258556691,"Long": 106.7755310560839}],"Name": "Water",}];

const output = data.reduce((accumulator, curr) => {
  if(curr.Coordinates) {
    const Coordinates = curr.Coordinates.map(({Lat, Long}) => [Lat, Long]);
    accumulator.push({ Name: curr.Name, Coordinates});
  };
  return accumulator;
}, []);

console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

1

Just update your reduce a little bit.

Rather than pushing the coordinates array directly to the accumulator, make it as an object with name and coords as keys. Push this object to the accumulator to get your desired output.

const data = [{"Type": "Location","Name": "Water",},{"Type": "Location","Coordinates": [{"Lat": 57.94182777439993,"Long": 79.50404114815193},{"Lat": 31.209277877460135,"Long": 78.80122177677728},{"Lat": 31.35950051982242,"Long": 105.15694820332524},{"Lat": 58.17432360099434,"Long": 105.42050546759074}],"Name": "Water",},{"Type": "Location","Coordinates": [{"Lat": 58.72972797827911,"Long": 76.90570777266291},{"Lat": 29.54717721331581,"Long": 76.37859324413196},{"Lat": 30.460511875130663,"Long": 105.19418747049103},{"Lat": 59.71902258556691,"Long": 106.7755310560839}],"Name": "Water",}];

const output = data.reduce((accumulator, curr) => {
  if (curr.Coordinates) {
    const data = {
      name: curr.Name,
      coords: curr.Coordinates.map(({ Lat, Long }) => [Lat, Long]),
    }
    accumulator.push(data)
  };
  return accumulator;
}, []);

console.log(output);

1 Comment

@SaiKrishnadas You are welcome, please approve the answer if this is what you are looking for.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.