2

I have an API which is formatted like:

[
   {
      "id": 4,
      "code": "IN",
      "name": "India",
      "recordVersion": "6CB5C760-E145-4FFA-AC63-FAF5F36B6C80",
      "updatedBy": "Sini "
   },
   {
      "id": 59,
      "code": "UK",
      "name": "United Kingdom",
      "recordVersion": "3512B084-8DB7-48F6-9E01-C0861E2A128D",
      "updatedBy": "sinju "
   }
]

I need to convert it into this format:

localizedData = [ 
    { 
        text: "India", 
        value: 4 
    }, 
    { 
        text: "United Kingdom", 
        value: 59 
    }
];

How can I do that?

0

5 Answers 5

2

Assuming you want to map name property to text, and id property to value.

const source = [
  {
    "id": 4,
    "code": "IN",
    "name": "India",
    "recordVersion": "6CB5C760-E145-4FFA-AC63-FAF5F36B6C80",
    "updatedBy": "Sini "
  },
  {
    "id": 59,
    "code": "UK",
    "name": "United Kingdom",
    "recordVersion": "3512B084-8DB7-48F6-9E01-C0861E2A128D",
    "updatedBy": "sinju "
  }
];

const localizedData = source.map(item => ({text: item.name, value: item.id}));

console.log(localizedData )

Learning about the Array.prototype.map() will help you next time.

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

Comments

0

It will help you-----

             var results = data;
               var arr = [], item;
                 for (var i = 0, len = results.length; i < len; i++) {
                      item = results[i];
                      arr.push({username: item.username, description: item.description, 
                         name: 
                        item.name});
                               }

Comments

0

Say your result array is:

const res =  { "id": 4, "code": "IN", "name": "India", "recordVersion": "6CB5C760-E145-4FFA-AC63-FAF5F36B6C80", "updatedBy": "Sini " }, { "id": 59, "code": "UK", "name": "United Kingdom", "recordVersion": "3512B084-8DB7-48F6-9E01-C0861E2A128D", "updatedBy": "sinju " } }

All you need to do is:

const res2 = res.map(a => ({text: a.name, value: a.id}));

That should work

1 Comment

Minor syntax issue: res.map(a => ({text: a.name, value: a.id}) or it will map to undefined.
0

You can try this.

const data = [
  {
    "id": 4,
    "code": "IN",
    "name": "India",
    "recordVersion": "6CB5C760-E145-4FFA-AC63-FAF5F36B6C80",
    "updatedBy": "Sini "
  },
  {
    "id": 59,
    "code": "UK",
    "name": "United Kingdom",
    "recordVersion": "3512B084-8DB7-48F6-9E01-C0861E2A128D",
    "updatedBy": "sinju "
  }
];

const localizedData = [];

for(i=0;i< data.length;i++){
localizedData.push({text : data[i].name, value : data[i].id})
}

console.log(localizedData)

Comments

0

Try this :

Solution 1 :

var jsonObj = [
   {
      "id": 4,
      "code": "IN",
      "name": "India",
      "recordVersion": "6CB5C760-E145-4FFA-AC63-FAF5F36B6C80",
      "updatedBy": "Sini "
   },
   {
      "id": 59,
      "code": "UK",
      "name": "United Kingdom",
      "recordVersion": "3512B084-8DB7-48F6-9E01-C0861E2A128D",
      "updatedBy": "sinju "
   }
];

jsonObj.map((obj) => {
	obj["value"] = obj["id"];
  obj["text"] = obj["name"];
  delete obj.id;
  delete obj.code;
  delete obj.name;
  delete obj.recordVersion;
  delete obj.updatedBy;
});

console.log(jsonObj);

Solution 2 :

const jsonObj = [
  {
    "id": 4,
    "code": "IN",
    "name": "India",
    "recordVersion": "6CB5C760-E145-4FFA-AC63-FAF5F36B6C80",
    "updatedBy": "Sini "
  },
  {
    "id": 59,
    "code": "UK",
    "name": "United Kingdom",
    "recordVersion": "3512B084-8DB7-48F6-9E01-C0861E2A128D",
    "updatedBy": "sinju "
  }
];

const res = [];

for (var i of jsonObj) {
  res.push({value : i.id, text : i.name});
}

console.log(res);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.