javascript convert array to json object code example
Example 1: js array to json
const jsonString = JSON.stringify(yourArray);
const yourData = JSON.parse(jsonString);
Example 2: how to convert array to string json
var arr = [ "John", "Peter", "Sally", "Jane" ];
var myJSON = JSON.stringify(arr);
Example 3: javascript convert array to object
function arrayToObject(arr) {
var obj = {};
for (var i = 0; i < arr.length; ++i){
obj[i] = arr[i];
}
return obj;
}
var colors=["red","blue","green"];
var colorsObj=arrayToObject(colors);
Example 4: how to use json stringify in javascript
var Num=[1,2,3,4,5,6]
console.log("The Numbers Are "+JSON.stringify(Num))