现有这样一段json,问怎么才能处理成为我想要的格式呢?

现有这样一段json,问怎么才能处理成为我想要的格式呢?


        articleList:[

                {id:1,name:"小红",user:456312,Occupation:"教练"},

                {id:2,name:"小张",user:456321,Occupation:"歌手"},

                {id:3,name:"小强",user:456312,Occupation:"老师"},

                {id:1,name:"小分",user:456312,Occupation:"同事"},

                {id:2,name:"小撒",user:456132,Occupation:"总经理"},

                {id:3,name:"小看",user:456132,Occupation:"程序猿"},

                {id:1,name:"小贫",user:451632,Occupation:"演员"}

            ],

            

            

            

把id相同的放在一个数组里面,用js代码怎么实现呢?大神


        articleList:[

                {

                    id:1,

                    list:[

                            {name:"小红",user:456312,Occupation:"教练"},

                            {name:"小分",user:456312,Occupation:"同事"}.

                            {name:"小贫",user:451632,Occupation:"演员"}

                     ]

                },

                {

                    id:2,

                    list:[

                            {name:"小张",user:456321,Occupation:"歌手"},

                            {name:"小撒",user:456132,Occupation:"总经理"}

                     ]

                },

                {

                    id:,

                    list:[

                            {name:"小强",user:456312,Occupation:"老师"},

                            {name:"小看",user:456132,Occupation:"程序猿"}

                     ]

                }

            ]


郎朗坤
浏览 527回答 3
3回答

慕侠2389804

这一段可以实现你说的功能。比较简单,就是按id往数组对应的下标里写东西就好了。let articleList = [&nbsp; &nbsp; { id: 1, name: "小红", user: 456312, Occupation: "教练" },&nbsp; &nbsp; { id: 2, name: "小张", user: 456321, Occupation: "歌手" },&nbsp; &nbsp; { id: 3, name: "小强", user: 456312, Occupation: "老师" },&nbsp; &nbsp; { id: 1, name: "小分", user: 456312, Occupation: "同事" },&nbsp; &nbsp; { id: 2, name: "小撒", user: 456132, Occupation: "总经理" },&nbsp; &nbsp; { id: 3, name: "小看", user: 456132, Occupation: "程序猿" },&nbsp; &nbsp; { id: 1, name: "小贫", user: 451632, Occupation: "演员" }];let newObject = [];//循环for (let index in articleList) {&nbsp; &nbsp; //取对象&nbsp; &nbsp; let o = articleList[index];&nbsp; &nbsp; //第一次为一个id增加对象&nbsp; &nbsp; if (!newObject[o.id]) {&nbsp; &nbsp; &nbsp; &nbsp; newObject[o.id] = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: o.id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list: []&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //加入进去&nbsp; &nbsp; newObject[o.id].list.push({&nbsp; &nbsp; &nbsp; &nbsp; name: o.name,&nbsp; &nbsp; &nbsp; &nbsp; user: o.user,&nbsp; &nbsp; &nbsp; &nbsp; Occupation: o.Occupation&nbsp; &nbsp; })}//打印结果console.log(newObject);以上代码会在数组里产生空值,使用for(let i = 0;i<a.length;i++)遍历会取出undefine。其实使用for in遍历就不会出现这个问题。可以增加代码如下:&nbsp; &nbsp; let array = [];&nbsp; &nbsp; for (let key in newObject){&nbsp; &nbsp; &nbsp; array.push(newObject[key])&nbsp; &nbsp; }&nbsp; &nbsp; console.log('array',array);可以看到是可以正常遍历的,即使id是字符串,上述代码也不会有问题,因为数组也可以使用字符串作为下标。

哔哔one

let foldById = data => data.reduce((acc, cur) => {&nbsp; &nbsp; let { id, name, user, Occupation } = cur;&nbsp;&nbsp; &nbsp; let toInsert = { name, user, Occupation };&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if (acc[id]){&nbsp; &nbsp; &nbsp; &nbsp; acc[id].list.push(toInsert);&nbsp;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; acc[id] = { id, list: [ toInsert ] }&nbsp; &nbsp; }&nbsp; &nbsp; return acc;&nbsp;}, {});结果是类数组对象。testlet data = [&nbsp; &nbsp; { id: 1, name: "小红", user: 456312, Occupation: "教练" },&nbsp; &nbsp; { id: 2, name: "小张", user: 456321, Occupation: "歌手" },&nbsp; &nbsp; { id: 3, name: "小强", user: 456312, Occupation: "老师" },&nbsp; &nbsp; { id: 1, name: "小分", user: 456312, Occupation: "同事" },&nbsp; &nbsp; { id: 2, name: "小撒", user: 456132, Occupation: "总经理" },&nbsp; &nbsp; { id: 3, name: "小看", user: 456132, Occupation: "程序猿" },&nbsp; &nbsp; { id: 1, name: "小贫", user: 451632, Occupation: "演员" }];foldById(data);&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript