8

Dynamically I am getting an array.

For example we can consider this following array.

var sampleArray=[
        "logo",
        "Details",
        "titles"
    ];

But I want it something like this.

jsonObj={
"poistion1":"logo",
"poistion2":"Details",
"poistion3":"titles"
}
Shrinivas Pai
  • 7,491
  • 4
  • 29
  • 56
  • var sampleArray=[ "logo", "Details", "titles" ]; String[] stringArray = Arrays.copyOf(sampleArray, sampleArray.length, String[].class); JSONArray mJSONArray = new JSONArray(Arrays.asList(stringArray)); for(int n = 0; n < mJSONArray.length(); n++) { JSONObject object = mJSONArray.getJSONObject(n); // do some stuff.... } – ask4solutions Jul 14 '15 at 06:28

6 Answers6

4

You can iterate on array and create object like following

var jsonObj = {};
for (var i = 0 ; i < sampleArray.length; i++) {
    jsonObj["position" + (i+1)] = sampleArray[i];
}
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
4

Like this

var jsonObj = {};

var sampleArray = [
    "logo",
    "Details",
    "titles"
];

for (var i = 0, len = sampleArray.length; i < len; i++) {
    jsonObj['position' + (i + 1)] = sampleArray[i];
}

console.log(jsonObj);
Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
3

You can create an empty object, then loop over(Array.forEach()) the array and assign the value

var sampleArray = [
  "logo",
  "Details",
  "titles"
];
var obj = {};
sampleArray.forEach(function(value, idx) {
  obj['position' + (idx + 1)] = value
});

snippet.log(JSON.stringify(obj))
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • forEach will not be supported in ie lower versions – Raghavendra Jul 14 '15 at 06:25
  • @raghavendra there is [polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill) for that purpose – Arun P Johny Jul 14 '15 at 06:27
  • asking person don't even know basics about loops. better we can teach them basics loops which will support in standards. – Raghavendra Jul 14 '15 at 06:28
  • would you pls explain about polyfill. does it support in ie6? – Raghavendra Jul 14 '15 at 06:29
  • It's 2015, I thought by now we would be beyond asking about support for the officially dead IE6...around only 1.7% of the world uses a browser (old IE) that does not support forEach (ECMAScript 5) – Kabb5 Aug 05 '15 at 18:29
2
var arr=[
        "logo",
        "Details",
        "titles"
    ];
var result = {};
  for (var i = 0; i < arr.length; ++i){
      result["position" + (i+1)] = arr[i];
 }
Nikhil Batra
  • 3,118
  • 14
  • 19
1

You can use the JSON Object:

var yourObject = [123, "Hello World", {name: "Frankie", age: 15}];
var yourString = JSON.stringify(yourObject); // "[123,"Hello World",{"name":"Frankie","age":15}]"

the JSON object has also JSON-to-Object functionality:

var anotherObject = JSON.parse(yourString);

Cerlin
  • 6,622
  • 1
  • 20
  • 28
G_hi3
  • 588
  • 5
  • 22
  • the input is in array, and the required output is in json format with ` jsonObj={ "poistion1":"logo", "poistion2":"Details", "poistion3":"titles" } ` Is your output working and showing the correct output? I dont think so – Rohan Devaki Dec 04 '20 at 07:12
0

try this

var obj = {};
var sampleArray=[
        "logo",
        "Details",
        "titles"
    ];

for(var index in  sampleArray) {
     obj['pos' + index] = sampleArray[index];
   }
Raghavendra
  • 3,530
  • 1
  • 17
  • 18