I have an json array like this
var data= [
{
"id": 24,
"title": "BCOM",
"start": "2014-08-05 12:59:00 PM",
"end": "2014-08-05 2:59:00 PM",
"description": "mcom",
"DIFF": 120
},
{
"id": 26,
"title": "MCOM",
"start": "2014-08-10 12:59:00 PM",
"end": "2014-08-10 4:59:00 PM",
"description": "mcom",
"DIFF": 240
},
{
"id": 29,
"title": "MCOM",
"start": "2014-08-11 12:59:00 PM",
"end": "2014-08-11 8:59:00 PM",
"description": "mcom",
"DIFF": 480
},
{
"id": 30,
"title": "MCOM",
"start": "2014-08-13 12:59:00 PM",
"end": "2014-08-13 4:59:00 PM",
"description": "mcom",
"DIFF": 240
}
]
I want to make this array having index with out double quotes and change some index names , and some other modifications by using javascript array functions.
like
var data = [
{
id: 24,
title:"MCOM",
y: 120
},
{
id: 26,
title:"MCOM",
y: 240,
},
{
id: 29,
title:"MCOM",
y: 480,
},
]
Please help me in this, Thank you
3 Answers 3
Indexes with quotes ({ "title": "asdf" }) are equivalent to indexes without quotes ({ title: "asdf" }) in javascript, except that you can use more symbols like spaces, brackets or keywords in the quoted version.
Also, in JSON, you HAVE to add quotes around indexes, otherwise it's not valid JSON.
About the modifications, you can use Array.prototype.map() to do this
var newData = data.map(function (el) {
return {
id: el.id,
title: el.title,
y: el.DIFF
};
});
console.log(newData);
Comments
Use Array map function for this
var result = data.map(function (obj) {
return {id:obj.id,title:obj.title,y:obj.DIFF};
})
console.log(result);
I'm assuming in the result there should be BCOM as title for id, 24. I this is a type that there it's written as MCOM.
what I think "" shouldn't matter for keys.
Comments
Do some googling ;-) it will help..
var data = '{ "name": "John Smith" }'; //Let's say you got this
data = data.replace(/\"([^(\")"]+)\":/g,"1ドル:"); //This will remove all the quotes
data; //'{ name: "John Smith" }'
Hope it helps.
ywith the value ofDIFFanddeleteother attributes ! You can useArray.prototype.forEach()and code a callback function to do your modifications.BCOMisn't it?