1

The code below produces an output as shown below which is wrong as per my expectation.

Wrong format

The format I expect to have is shown below. How can I achieve this? What am I doing wrong?

I try and drill-down the structure to get a list of records from dealerListItemDToes array however I can't figure out a way to have it in the structure below.

CorrectFormat

let responseData = {
  "_embedded": {
    "dealerListItemDToes": [
      {
        "id": "ab4b23bb-b58d-4012-93ea-702e37ea555a",
        "gssnID": "GSM00000001",
        "companyID": "GCM00000001",
        "mbID": "41010f",
        "name": "GINO RAG. FELICE & FIGLIO - S.p.A.",
        "website": "http://www.gino.mercedes-benz.it",
        "phone": "+39 0171 410777",
        "country": "Italy",
        "city": "Madonna",
        "postalCode": "12100",
        "address": "Madonna dellolmo",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PVU",
        "passengerCar": true,
        "van": true,
        "bus": false,
        "unimog": true,
        "truck": false
      },
      {
        "id": "b644a324-6858-4ac9-a2ce-a4539cee43a5",
        "gssnID": "GSM00000002",
        "companyID": "GCM00000002",
        "mbID": "42010e",
        "name": "L-AUTO. S.r.l.",
        "website": "http://www.lauto.mercedes-benz.it",
        "phone": "+39 0775 8859500",
        "country": "Italy",
        "city": "Frosinone",
        "postalCode": "3100",
        "address": "Via Enrico Fermi, 25",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PTBU",
        "passengerCar": true,
        "van": false,
        "bus": true,
        "unimog": true,
        "truck": true
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "self": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "next": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=1&size=10&sort=gssnID,asc"
    },
    "last": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=2&size=10&sort=gssnID,asc"
    }
  },
  "page": {
    "size": 10,
    "totalElements": 27,
    "totalPages": 3,
    "number": 0
  }
};

function getDataRecords(providedData: any): Array<any> {
  const dataSet: Array<any> = Object.entries(providedData._embedded)[0];
  dataSet.splice(0, 1); // Delete first property as its name can vary
  return dataSet;
}

console.log(getDataRecords(responseData));
3
  • 2
    Are you using just Javascript? because function getDataRecords(providedData: any): Array<any> seems to be another language, if it is, please, add the language tag to your question Commented Feb 21, 2020 at 12:22
  • @CalvinNunes It is TypeScript actually. Commented Feb 21, 2020 at 12:24
  • @HadidAli why you are not using providedData._embedded. dealerListItemDToes ? Commented Feb 21, 2020 at 12:27

1 Answer 1

1

I have converted your typescript into a Javascript code like

let responseData = {
  "_embedded": {
    "dealerListItemDToes": [
      {
        "id": "ab4b23bb-b58d-4012-93ea-702e37ea555a",
        "gssnID": "GSM00000001",
        "companyID": "GCM00000001",
        "mbID": "41010f",
        "name": "GINO RAG. FELICE & FIGLIO - S.p.A.",
        "website": "http://www.gino.mercedes-benz.it",
        "phone": "+39 0171 410777",
        "country": "Italy",
        "city": "Madonna",
        "postalCode": "12100",
        "address": "Madonna dellolmo",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PVU",
        "passengerCar": true,
        "van": true,
        "bus": false,
        "unimog": true,
        "truck": false
      },
      {
        "id": "b644a324-6858-4ac9-a2ce-a4539cee43a5",
        "gssnID": "GSM00000002",
        "companyID": "GCM00000002",
        "mbID": "42010e",
        "name": "L-AUTO. S.r.l.",
        "website": "http://www.lauto.mercedes-benz.it",
        "phone": "+39 0775 8859500",
        "country": "Italy",
        "city": "Frosinone",
        "postalCode": "3100",
        "address": "Via Enrico Fermi, 25",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PTBU",
        "passengerCar": true,
        "van": false,
        "bus": true,
        "unimog": true,
        "truck": true
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "self": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "next": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=1&size=10&sort=gssnID,asc"
    },
    "last": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=2&size=10&sort=gssnID,asc"
    }
  },
  "page": {
    "size": 10,
    "totalElements": 27,
    "totalPages": 3,
    "number": 0
  }
};

function getDataRecords(providedData) {
  const dataSet = Object.entries(providedData._embedded)[0];
  dataSet.splice(0, 1); // Delete first property as its name can vary
  return dataSet;
}

console.log(getDataRecords(responseData));

This results in

enter image description here

and the only problem is that the array that you expect is the first and only element of another array, wrapped around it. So, I solved it like this:

let responseData = {
  "_embedded": {
    "dealerListItemDToes": [
      {
        "id": "ab4b23bb-b58d-4012-93ea-702e37ea555a",
        "gssnID": "GSM00000001",
        "companyID": "GCM00000001",
        "mbID": "41010f",
        "name": "GINO RAG. FELICE & FIGLIO - S.p.A.",
        "website": "http://www.gino.mercedes-benz.it",
        "phone": "+39 0171 410777",
        "country": "Italy",
        "city": "Madonna",
        "postalCode": "12100",
        "address": "Madonna dellolmo",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PVU",
        "passengerCar": true,
        "van": true,
        "bus": false,
        "unimog": true,
        "truck": false
      },
      {
        "id": "b644a324-6858-4ac9-a2ce-a4539cee43a5",
        "gssnID": "GSM00000002",
        "companyID": "GCM00000002",
        "mbID": "42010e",
        "name": "L-AUTO. S.r.l.",
        "website": "http://www.lauto.mercedes-benz.it",
        "phone": "+39 0775 8859500",
        "country": "Italy",
        "city": "Frosinone",
        "postalCode": "3100",
        "address": "Via Enrico Fermi, 25",
        "description": "description",
        "createdBy": "DataAdmin",
        "updatedBy": "DataAdmin",
        "createdAt": null,
        "updatedAt": null,
        "business": "PTBU",
        "passengerCar": true,
        "van": false,
        "bus": true,
        "unimog": true,
        "truck": true
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "self": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=0&size=10&sort=gssnID,asc"
    },
    "next": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=1&size=10&sort=gssnID,asc"
    },
    "last": {
      "href": "http://localhost:8080/api/v1/dealers?direction=ASC&pageSize=10&page=2&size=10&sort=gssnID,asc"
    }
  },
  "page": {
    "size": 10,
    "totalElements": 27,
    "totalPages": 3,
    "number": 0
  }
};

function getDataRecords(providedData) {
  const dataSet = Object.entries(providedData._embedded)[0];
  dataSet.splice(0, 1); // Delete first property as its name can vary
  return dataSet[0];
}

console.log(getDataRecords(responseData));

Resulting in

enter image description here

If this solves the problem, then the only modification on your almost correct script is that on the return statement I have specified to return the 0th element of the outer array, which is the result that you expect.

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

2 Comments

I have forgot to mention that property "dealerListItemDToes" can vary and thus should not be hard-coded in the solution.
@HadidAli can you provide an example where this is a problem?

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.