0

Noob here, Can anyone please help me convert this Object which I am receiving in API Call to an array as mentioned below, also this Object will be dynamic i.e. record length can change. Thanks in advance.

myObject = {
  "records": [{
      "id": "recDBqsW7C3Dk3HMd",
      "fields": {
        "Latitude": "24.898907",
        "Longitude": "67.117303",
        "CustomerName": "Asad"
      },
      "createdTime": "2020-10-07T04:43:31.000Z"
    },
    {
      "id": "recGlfTbUcEvP46Lf",
      "fields": {
        "Latitude": "24.907641",
        "Longitude": "67.1088035",
        "CustomerName": "Umar"
      },
      "createdTime": "2020-10-07T04:44:11.000Z"
    },
    {
      "id": "recfsQsoznDyWPDe8",
      "fields": {
        "Latitude": "24.911112",
        "Longitude": "67.105117",
        "CustomerName": "Ali"
      },
      "createdTime": "2020-10-06T09:11:05.000Z"
    }
  ]
};

To something like:

myArray = [{
    "lat": 24.898907,
    "lon": 67.117303,
    "name": "Asad"
  },
  {
    "lat": 24.907641,
    "lon": 67.1088035,
    "name": "Umar"
  },
  {
    "lat": 24.911112,
    "lon": 67.105117,
    "name": "Ali"
  }
]
1
  • Show us what you have tried. SO isn't a free code writing service. The objective here is for you to post your attempts to solve your own issue and others help when they don't work as expected. See How to Ask and minimal reproducible example Commented Nov 11, 2020 at 3:45

2 Answers 2

4

This can be done using Array.prototype.map function.

const input = {
  "records": [{
      "id": "recDBqsW7C3Dk3HMd",
      "fields": {
        "Latitude": "24.898907",
        "Longitude": "67.117303",
        "CustomerName": "Asad"
      },
      "createdTime": "2020-10-07T04:43:31.000Z"
    },
    {
      "id": "recGlfTbUcEvP46Lf",
      "fields": {
        "Latitude": "24.907641",
        "Longitude": "67.1088035",
        "CustomerName": "Umar"
      },
      "createdTime": "2020-10-07T04:44:11.000Z"
    },
    {
      "id": "recfsQsoznDyWPDe8",
      "fields": {
        "Latitude": "24.911112",
        "Longitude": "67.105117",
        "CustomerName": "Ali"
      },
      "createdTime": "2020-10-06T09:11:05.000Z"
    }
  ]
};

const output = input.records.map(({ fields }) => ({
  lat: fields.Latitude,
  lan: fields.Longitude,
  name: fields.CustomerName
}));
console.log(output);

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

2 Comments

This works actually, however, our inhouse platform does not support map function. is there any other way to do this?
You need to loop using for and add one by one manually then. like this. const output = []; for (let i = 0; i < input.records.length; i ++) { output.push({ lat: input.records[i].fields.Latitude, lan: input.records[i].fields.Longitude, name: input.records[i].fields.CustomerName }); } console.log(output);
0

You could use a for...of loop if the map function is not supported by your platform.

const input = {
  "records": [{
      "id": "recDBqsW7C3Dk3HMd",
      "fields": {
        "Latitude": "24.898907",
        "Longitude": "67.117303",
        "CustomerName": "Asad"
      },
      "createdTime": "2020-10-07T04:43:31.000Z"
    },
    {
      "id": "recGlfTbUcEvP46Lf",
      "fields": {
        "Latitude": "24.907641",
        "Longitude": "67.1088035",
        "CustomerName": "Umar"
      },
      "createdTime": "2020-10-07T04:44:11.000Z"
    },
    {
      "id": "recfsQsoznDyWPDe8",
      "fields": {
        "Latitude": "24.911112",
        "Longitude": "67.105117",
        "CustomerName": "Ali"
      },
      "createdTime": "2020-10-06T09:11:05.000Z"
    }
  ]
};

const output = [];

for(let record of input.records){
    output.push({
        lat: record.fields.Latitude,
        lan: record.fields.Longitude,
        name: record.fields.CustomerName
    })
}

console.log(output);

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.