2

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.

4 Answers 4

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:

enter image description here

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]));
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please make it brief, im new for javascript,
What do you mean by brief? That is as brief as it gets. It is a one line solution.
Ok i will try it
1

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.

Comments

0

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.

Comments

-1

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); 

enter image description here

3 Comments

How to get data from json file?
you can use jQuery $.getJSON() like below : $.getJSON("Data.json", function(data) { console.log(data); });
Solution works but doesn't follow functional principles or leverage ES6 language features (like Array.map).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.