Skip to content

Instantly share code, notes, and snippets.

@tanshio
Last active August 29, 2015 14:04
Show Gist options
  • Save tanshio/31e97a56a1abaf553a1d to your computer and use it in GitHub Desktop.
Save tanshio/31e97a56a1abaf553a1d to your computer and use it in GitHub Desktop.
json.parse
shop = {};
shop.chara ={
'elmo':{
'name':'えるも',
'item':'あいす',
'itemList':{
'ice':{
'name':'アイス',
'list':[
//商品名,商品説明,svg色配列['class','プロパティ','値']
['バニラアイス','バニラあじのアイス', [['outline','stroke','#ccc'],['top','fill','#ccc'],['bottom','fill','#000']]],
['ストロベリーアイス','イチゴあじのアイス',[['outline','stroke','red'],['top','fill','#ccc'],['bottom','fill','#000']]],
['チョコアイス','チョコあじのアイス', [['outline','stroke','#000'],['top','fill','#ccc'],['bottom','fill','#000']]],
['メロンアイス','メロンあじのアイス', [['outline','stroke','green'],['top','fill','#ccc'],['bottom','fill','#000']]],
['みかんアイス','みかんあじのアイス', [['outline','stroke','orange'],['top','fill','#ccc'],['bottom','fill','#000']]]
]
},
'chipice':{
'name':'アイス',
'list':[
['チョコチップミントアイス','ミントアイスのなかにチョコチップがはいったアイス',[['outline','stroke','#ccc'],['inner','fill','green']]],
['スイカアイス','スイカあじのアイスのなかにチョコがはいったアイス', [['outline','stroke','#ccc'],['inner','fill','green']]],
['クッキーバニラ','バニラアイスの中にクッキーがはいったアイス', [['outline','stroke','#ccc'],['inner','fill','brown']]]
]
}
}
}
};
var charaArr =[];
function charaArrSet(prop,obj){
// すべてのアイテムを走査し、配列に変換する
var arr = [];
// キーを配列の最初にに加えて作成したい
for (var i = 0; obj.list.length>i; i++) {
arr[i]=JSON.parse(JSON.stringify(obj.list[i]));
arr[i].unshift(prop);
}
// charaArrにセット
for (var j = 0; arr.length>j; j++) {
charaArr.push(arr[j]);
}
}
var character = shop.chara.elmo;
for (var prop in character.itemList) {
if (character.itemList.hasOwnProperty(prop)) {
var obj = character.itemList[prop];
charaArrSet(prop,obj);
}
}
console.log(charaArr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment