Looking at this intricate JSON object...
Here's a snippet of the code:
entity: [{entityName: "Nrm", page: 0, pageSize: 241, status: "successfully perfrom: select Operation",…}]
0: {entityName: "Nrm", page: 0, pageSize: 241, status: "successfully perfrom: select Operation",…}
entityName: "Nrm"
page: 0
pageSize: 241
rows: [{columns: [{paramName: "country_alpha3_code", paramValue: "PLW", dataType: "String"},…]},…]
[0 … 99]
0: {columns: [{paramName: "country_alpha3_code", paramValue: "PLW", dataType: "String"},…]}
columns: [{paramName: "country_alpha3_code", paramValue: "PLW", dataType: "String"},…]
0: {paramName: "country_alpha3_code", paramValue: "PLW", dataType: "String"}
dataType: "String"
paramName: "country_alpha3_code"
paramValue: "PLW"
1: {paramName: "country_name", paramValue: "PALAU", dataType: "String"}
dataType: "String"
paramName: "country_name"
paramValue: "PALAU"
2: {paramName: "country_alpha2_code", paramValue: "PW", dataType: "String"}
dataType: "String"
paramName: "country_alpha2_code"
paramValue: "PW"
Here's a visual representation with a script snippet:
https://i.sstatic.net/f0o41.png
The objective is to create a simple JSON Object for a country Drop Down without the need for a custom API, using Typescript and Lodash
countries = {
[{
name: "United States",
code: "US"
},{
name: "Canada",
code: "CN"
},{
name: "Italy",
code: "IT"
},
....]
}
That's the concept.
Following is my typescript function where I intend to generate the new JSON Object
async getCountryOptions() {
// this.services.getCountries();
const response = await this.services.getCountries();
// console.log('RESPONSE AFTER AWAIT: ', response);
this.countries = this.services.countryList;
console.log('Did we get countries: ', this.countries);
// Now to create a new array for only country code and name
****************** HERE *********************
// I TRIED THIS but I got ...val is not an iterator
this.newJSONCountryArr = Object.entries(this.countries)
.map(([key, val]) => [...val, key]);
console.log('Result: ', this.newJSONCountryArr);
if (this.countries) {
this.meta.countryOptions.push(this.services.countryList);
} else {
console.log('Whoops! ')
}
}