0

I'm receiving a response from json in the below format

["user0", "user1", "user2", "user3", "user5", "user6", "user7"]

and i need to convert it into the below Format

    [
     { id: 'user0', text: 'user1'},
     { id: 'user1', text: 'user2'},
     { id: 'user2', text: 'user3'},
     { id: 'user3', text: 'user4'},
     { id: 'user5', text: 'user5'},
     { id: 'user6', text: 'user6' },
     { id: 'user7', text: 'user7' }
    ];

How this can be achieved in javascript.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
rkj
  • 541
  • 3
  • 12
  • 25

4 Answers4

0

A simple loop should do what you need:

var myArray = ["user0", "user1", "user2", "user3", "user5", "user6", "user7"];
var newArray = [];
for (var i = 0, l = myArray.length; i < l; i++) {
    var newItem = {id : myArray[i], text: "user" + (i +1)};
    newArray.push(newItem);
}

JSFiddle Example - http://jsfiddle.net/eVQWS/

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

The easiest way is to use map method:

var arr = ["user0", "user1", "user2", "user3", "user5", "user6", "user7"],
    result = arr.map(function(e, i, a) {
        return { id: e, text: i < a.length - 1 ? a[i+1] : e };
    });

console.log(result);

Please check for browser compatibility at MDN and use shim if needed.

VisioN
  • 143,310
  • 32
  • 282
  • 281
0

In a modern browser I would use map.

var array = ["user0", "user1", "user2", "user3", "user5", "user6", "user7"];

var newArray = array.map(function(item) {
    return {id: item, text: item};
})

If you have to support old browsers with no ES5 supports, I would use a shim – usually I shim all the ES5 methods, but you can also implement only the one you need, an implementation example is in the docs.

Just to know, in the ES6 – and Firefox Nightly that already support the new fat array syntax – you could actually have:

var array = ["user0", "user1", "user2", "user3", "user5", "user6", "user7"];

var newArray = array.map(item => ({id: item, text: item}));
ZER0
  • 24,846
  • 5
  • 51
  • 54
0

Try this

    var a = ["user0", "user1", "user2", "user3", "user5", "user6", "user7"]
    var array = [];

    for(var i =0;i<a.length;i++)
    {
        var temp = new Object();
        temp.id= a[i];
        temp.text = a[i];
        array.push(temp);
        //alert(JSON.stringify(array));
    }

var requiredString = JSON.stringify(array);
alert(requiredString.replace(/"id"/g, 'id').replace(/"text"/g,text));
PranitG
  • 121
  • 8
  • Thanks @PranitG!! The output for above is [{"id":"user0","text":"user0"},{"id":"user1","text":"user1"},{"id":"user2","text":"user2"},{"id":"user3","text":"user3"},{"id":"user5","text":"user5"},{"id":"user6","text":"user6"},{"id":"user7","text":"user7"}] i want id and text without quotes, would that be possible. – rkj Apr 12 '13 at 10:46
  • yes. try it var requiredString = JSON.stringify(array); alert(requiredString.replace(/"id"/g, 'id').replace(/"text"/g,text)); – PranitG Apr 12 '13 at 10:54
  • Please see my edit in answer. You need to use `replace` function – PranitG Apr 12 '13 at 10:56
  • @rohitj: The quotation marks are just part of the representation of the data. They are not actually part of the data itself. If you want the data as JSON, then the quotation marks are *required*. Also don't forget to accept an answer. – Felix Kling Apr 12 '13 at 12:39
  • You can achieve both the things. If you want quotation do not use `replace` which i mentioned. If you want quotation use 'replace' like indicated. Still it is better to use it with quotation because it is default formatting for JSON – PranitG Apr 12 '13 at 12:41