JS(JQUERY)把数组数据变成JSON数据 5
varoldArr=[{"date":"20140929","value":"value1"},{"date":"20140929","value":"value2"},...
var oldArr = [{"date":"20140929","value":"value1"},
{"date":"20140929","value":"value2"},
{"date":"20140929","value":"value3"},
{"date":"20140930","value":"value4"},
{"date":"20140930","value":"value5"}];
根据“date”字段,把原来数组想得到newJson = {"20140929":{"date":"20140929","data":[{"date":"20140929","value":"value1"},"date":"20140929","value":"value2"},{"date":"20140929","value":"value3"}]},
"20140930":{"date":"20140930","data":[{"date":"20140930","value":"value4"},"date":"20140929","value":"value5"}]}};这种类型的数据,请问用JS(jQuery)怎么实现? 展开
{"date":"20140929","value":"value2"},
{"date":"20140929","value":"value3"},
{"date":"20140930","value":"value4"},
{"date":"20140930","value":"value5"}];
根据“date”字段,把原来数组想得到newJson = {"20140929":{"date":"20140929","data":[{"date":"20140929","value":"value1"},"date":"20140929","value":"value2"},{"date":"20140929","value":"value3"}]},
"20140930":{"date":"20140930","data":[{"date":"20140930","value":"value4"},"date":"20140929","value":"value5"}]}};这种类型的数据,请问用JS(jQuery)怎么实现? 展开
展开全部
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>JS的TAB切换</title>
<style type="text/css">
</style>
<script type="text/javascript">
var oldArr = [
{
"date" : "20140929",
"value" : "value1"
},
{
"date" : "20140929",
"value" : "value2"
},
{
"date" : "20140929",
"value" : "value3"
},
{
"date" : "20140930",
"value" : "value4"
},
{
"date" : "20140930",
"value" : "value5"
}
];
var convert = function (oldArr)
{
var newJson = {};
for ( var i = 0; i < oldArr.length; i++)
{
if (!newJson[oldArr[i].date])
{
newJson[oldArr[i].date] =
{
"date" : oldArr[i].date
};
}
if (!!newJson[oldArr[i].date]["data"])
{
newJson[oldArr[i].date]["data"].push (oldArr[i]);
}
else
{
newJson[oldArr[i].date]["data"] = [oldArr[i]];
}
}
return newJson;
}
var newJson = convert (oldArr);
console.log (newJson);
</script>
</head>
<body>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的代码有点问题, 不过可以先告诉你怎么讲js数组转换成json数据
下载一个JSON2文件, 然后使用 var json=JSON2.stringif(js数组);
下载一个JSON2文件, 然后使用 var json=JSON2.stringif(js数组);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
逻辑混乱!
newJson里到底是什么逻辑?自己把括号对齐了看看,结构理清楚再问问题。
newJson里到底是什么逻辑?自己把括号对齐了看看,结构理清楚再问问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询