JSON rearrangement
JSON rearrangement
本文关键字:rearrangement JSON 更新时间:2023-09-26
我有下面的对象,我想重新排列它,以便传递给模板。需要帮助从以下格式转换
var nature = [
{
"type": "fruit",
"name": "apple",
"color": "red",
},
{
"type": "vegetable",
"name": "carrot",
"color": "orange",
},
{
"type": "fruit",
"name": "grape",
"color": "green",
},
{
"type": "vegetable",
"name": "tamato",
"color": "red",
},
];
转换为这种格式的
var nature = [{
"fruit": [
{
"type": "fruit",
"name": "apple",
"color": "red"
},
{
"type": "fruit",
"name": "grape",
"color": "green"
}
],
"vegetable": [
{
"type": "vegetable",
"name": "carrot",
"color": "orange"
},
{
"type": "vegetable",
"name": "tomato",
"color": "red"
}
]
}];
正在寻找实现它的最快方法。感谢您查看该帖子。感谢您的帮助。
对抗大自然
var goAgainstNature = function(oldNat) {
var newNat = {}, i;
for(i = 0; i < oldNat.length; i++) {
(newNat[oldNat.type] = (newNat[oldNat.type] || [])).push(oldNat[i]);
}
return [newNat];
};
我认为这将是正确的
var goAgainstNature = function(oldNat) {
var newNat = {}, i;
for(i = 0; i < oldNat.length; i++) {
(newNat["'"+oldNat[i].type+"'"] = (newNat[oldNat.type] || [])).push(oldNat[i]);
}
return [newNat];
};
相关文章:
- jQuery匹配JSON对象的部分文本
- 在循环中分配json值时,值被覆盖
- 需要帮助设置json数组
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用JQuery解析JSON嵌套数组
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 如何使用json将对象列表从java转换为javascript
- 如何使用 node.js 比较两个 json 数组
- 如何将JSON数据导入我的ejs模板
- 区分JSON中的矩阵和列表列表
- 不显示带有本地json文件数据的谷歌地图脚本
- 如何将json数据显示为html
- 如何为json对象中的段发送array[]
- 根据id将json数组组合为一个json数组
- Json数据包含日期和时间格式
- AngularJS-在JSON选择器中使用变量名
- JSON rearrangement