Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过javascript更改json数据_Javascript_Arrays_Json_Object - Fatal编程技术网

如何通过javascript更改json数据

如何通过javascript更改json数据,javascript,arrays,json,object,Javascript,Arrays,Json,Object,我有一个Json数据 var orign = { users: [ { name: "name1", id: "aaa1", classify: "depth1" }, { name: "name2", id: "aaa2", classify: "depth1" },

我有一个Json数据

   var orign = {
            users: [
                { name: "name1", id: "aaa1", classify: "depth1" },
                { name: "name2", id: "aaa2", classify: "depth1" },
                { name: "name3", id: "aaa3", classify: "depth2" },
            ]
        }
我想更改json数据 像这样

        var result = {
            "depth1" : [
                { name: "name1", id: "aaa1"},
                { name: "name2", id: "aaa2"},
            ],
            "depth2" : [
                { name: "name3", id: "aaa3"},
            ],
           
        }

如何通过Javascript更改此设置?

您可以使用

原点={
用户:[
{name:“name1”,id:“aaa1”,classify:“depth1”},
{name:“name2”,id:“aaa2”,classify:“depth1”},
{名称:“name3”,id:“aaa3”,分类:“depth2”},
]
}
res=来源.用户.减少((上一个,当前)=>{
常量{classify,…rest}=curr;
if(上一个hasOwnProperty(分类)){
上一步[分类]。下一步(休息);
}否则{
上一个[分类]=[休息];
}
返回上一个;
}, {});

控制台日志(res)这与我一年前发布的问题非常相似

希望我的回答能有所帮助

var原点={
用户:[
{name:“name1”,id:“aaa1”,classify:“depth1”},
{name:“name2”,id:“aaa2”,classify:“depth1”},
{名称:“name3”,id:“aaa3”,分类:“depth2”},
]
};
常数减速机=(附件,项目)=>{
如果(!acc[项目分类]){
acc[项目分类]=[];
} 
acc[item.classify].push({name:item.name,id:item.id})
返回acc
}
const result=origin.users.reduce(reducer,{});

console.log(result)
您当前是否有任何可以提供的代码?请注意
var orign={…}
不是JSON,而是JavaScript对象。