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"
}
1
  • 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.... } Commented Jul 14, 2015 at 6:28

6 Answers 6

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];
}
2
  • How about if you want all the keys to only read position?
    – blankface
    Commented Feb 1, 2018 at 0:29
  • @ZeroDarkThirty - Not clear. Can you please elaborate? Commented Feb 1, 2018 at 4:33
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);

0
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 -->

5
  • forEach will not be supported in ie lower versions Commented Jul 14, 2015 at 6:25
  • @raghavendra there is polyfill for that purpose Commented Jul 14, 2015 at 6:27
  • asking person don't even know basics about loops. better we can teach them basics loops which will support in standards. Commented Jul 14, 2015 at 6:28
  • would you pls explain about polyfill. does it support in ie6? Commented Jul 14, 2015 at 6: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
    Commented Aug 5, 2015 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];
 }
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);

1
  • 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 Commented Dec 4, 2020 at 7:12
0

try this

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

for(var index in  sampleArray) {
     obj['pos' + index] = sampleArray[index];
   }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.