Javascript 修改JSON对象中的值并将其放入另一个JSON对象中
我试图用以下方式更新现有的json对象,而不使用普通java脚本中的任何函数 附件是我的输入文件:Javascript 修改JSON对象中的值并将其放入另一个JSON对象中,javascript,arrays,json,Javascript,Arrays,Json,我试图用以下方式更新现有的json对象,而不使用普通java脚本中的任何函数 附件是我的输入文件: { "height_c": 534, "width_c": "800", "height_h": 1067, "width_h": "1600", "height_k": 1366, "width_k": "2048", "height_l": "683", "width_l": "1024", "height_m": "333", "width_m":
{
"height_c": 534,
"width_c": "800",
"height_h": 1067,
"width_h": "1600",
"height_k": 1366,
"width_k": "2048",
"height_l": "683",
"width_l": "1024",
"height_m": "333",
"width_m": "500",
"height_n": 213,
"width_n": "320",
"height_q": "150",
"width_q": "150",
"height_s": "160",
"width_s": "240",
"height_sq": 75,
"width_sq": 75,
"height_t": "67",
"width_t": "100",
"height_z": "427",
"width_z": "640"
}
附件是我的预期文件:
{
"sizes": {
"c": {
"width": 800,
"height": 534
},
"h": {
"width": 1600,
"height": 1067
},
"k": {
"width": 2048,
"height": 1366
},
"l": {
"width": 1024,
"height": 683
},
"m": {
"width": 500,
"height": 333
},
"n": {
"width": 320,
"height": 213
},
"q": {
"width": 150,
"height": 150
},
"s": {
"width": 240,
"height": 160
},
"q": {
"width": 75,
"height": 75
},
"t": {
"width": 100,
"height": 67
},
"z": {
"width": 640,
"height": 427
}
}
}
任何帮助都将不胜感激。我正在寻找一个起点。
var输入={
“高度c”:534,
“宽度c”:“800”,
“高度”:1067,
“宽度”:“1600”,
“高度”:1366,
“宽度k”:“2048”,
“高度”:“683”,
“宽度”:“1024”,
“高度m”:“333”,
“宽度m”:“500”,
“高度”:213,
“宽度”:“320”,
“高度”:“150”,
“宽度”:“150”,
“高度”:“160”,
“宽度”:“240”,
“高度”:75,
“宽度”_sq:75,
“高度”:“67”,
“宽度”:“100”,
“高度”:“427”,
“宽度_z”:“640”
}
变量输出={}
for(输入中的var键){
var split=键。拆分(“”)
如果(!output[split[1]])output[split[1]]={};
输出[split[1]][split[0]]=输入[key];
}
document.body.appendChild(document.createTextNode(JSON.stringify({size:output},null,4))代码>
body{white space:pre;font family:monospace;}
函数是纯JavascriptI中的一等公民,我怀疑不使用任何函数
实际上是指不使用任何第三方模块/lib。
没有函数,你甚至无法获得第一个json字符串,如果你修改了,你就不能修改它。@KevinB是的。。。。