最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to pass Json data into JavaScript Array? - Stack Overflow

programmeradmin4浏览0评论

I need to convert my json data into Javascript array, im using following data in Data.json file,

[{
        "nid": 1,
        "Desc": "Extra Style Window",
        "Xvalue": "448",
        "Yvalue": "458",
        "ImgValue": "1"
    },
    {
        "nid": 2,
        "Desc": "Door",
        "Xvalue": "138",
        "Yvalue": "558",
        "ImgValue": "2"
    },
    {
        "nid": 3,
        "Desc": "Fittings",
        "Xvalue": "400",
        "Yvalue": "258",
        "ImgValue": "3"
    },
    {
        "nid": 4,
        "Desc": "Fittings Spare",
        "Xvalue": "168",
        "Yvalue": "102",
        "ImgValue": "3"
    }
]

i want above data in following array format,

var dataPoints = new Array([nid,"Desc",Xvalue,Yvalue,ImgValue],......[n]);

i using bellow code for my side but its not working,

var arr = [];
for (var prop in data) {
    arr.push(data[prop]);
}
console.log(data.name);
alert(data.name);

How to convert it, i have huge amount of data in Json file, Please help me to fix this. If its have any other way to solve please let me know.

Thanks.

I need to convert my json data into Javascript array, im using following data in Data.json file,

[{
        "nid": 1,
        "Desc": "Extra Style Window",
        "Xvalue": "448",
        "Yvalue": "458",
        "ImgValue": "1"
    },
    {
        "nid": 2,
        "Desc": "Door",
        "Xvalue": "138",
        "Yvalue": "558",
        "ImgValue": "2"
    },
    {
        "nid": 3,
        "Desc": "Fittings",
        "Xvalue": "400",
        "Yvalue": "258",
        "ImgValue": "3"
    },
    {
        "nid": 4,
        "Desc": "Fittings Spare",
        "Xvalue": "168",
        "Yvalue": "102",
        "ImgValue": "3"
    }
]

i want above data in following array format,

var dataPoints = new Array([nid,"Desc",Xvalue,Yvalue,ImgValue],......[n]);

i using bellow code for my side but its not working,

var arr = [];
for (var prop in data) {
    arr.push(data[prop]);
}
console.log(data.name);
alert(data.name);

How to convert it, i have huge amount of data in Json file, Please help me to fix this. If its have any other way to solve please let me know.

Thanks.

Share Improve this question asked Aug 27, 2017 at 5:59 SathishkumarSathishkumar 4293 gold badges8 silver badges21 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You can do this quite simply using Javascript's map function:

var formattedArray = data.map(i => [i.nid, i.Desc, i.Xvalue, i.Yvalue, i.ImgValue]);

This will return an array containing arrays of each object's values:

And if you wanted a purely functional way to solve this problem (which could be reused on any array):

var formattedArray = data.map(item => Object.keys(item).map(i => item[i]));

In simply you can follow this example.

//var json_data= {name: "Dhana", age: 25};    
var json_data= [{"name":1,"age":7},{"name":5,"age":6}];
var result = [];

for(var i in json_data){
    var a=json_data[i]
    result.push(a);
   }

console.log(result[0].age);

if there is single object or multiple objects you can successfully put that objects into a array. Using .push() method only there will be problems in divide indexes within array. So I prefer this example and it gives proper solution for me.

var jsonObject = JSON.parse(string); // fetch your data in string and pass it.

jsonObject will be your array of items based on your json string. Now you can iterate jsonObject and access it's desire item/propety.

You can try following code to achieve required dataPoints array

var dataPoints = [];
for (var prop in data) {
    var arr = [];
    for(key in data[prop]){
        arr.push(data[prop][key]);
    }
    dataPoints.push(arr);
}   
console.log(dataPoints); 

发布评论

评论列表(0)

  1. 暂无评论