您好,登錄后才能下訂單哦!
這篇文章主要介紹js如何根據json數據中的某一個屬性來給數據分組,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
如下所示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <span id ="span" ></span> </body> <script> var arr = [{"Group":1,"Groupheader":"質量管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""}, {"Group":1,"Groupheader":"","Leftimg":"","Left":"","Min":"質量巡檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"設備管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備專業點檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備日檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備周檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備月檢","Right":"","Rightimg":""}]; var map = {}, dest = []; for(var i = 0; i < arr.length; i++){ var ai = arr[i]; if(!map[ai.Group]){ dest.push({ Group: ai.Group, data: [ai] }); map[ai.Group] = ai; }else{ for(var j = 0; j < dest.length; j++){ var dj = dest[j]; if(dj.Group == ai.Group){ dj.data.push(ai); break; } } } } console.log(JSON.stringify(dest)); var sapn = document.getElementById("span"); span.innerHTML = JSON.stringify(dest); </script> </html>
原始數據:
[{"Group":1,"Groupheader":"質量管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""}, {"Group":1,"Groupheader":"","Leftimg":"","Left":"","Min":"質量巡檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"設備管理","Leftimg":"","Left":"","Min":"","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備專業點檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備日檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備周檢","Right":"","Rightimg":""}, {"Group":2,"Groupheader":"","Leftimg":"","Left":"","Min":"設備月檢","Right":"","Rightimg":""}];
處理后的數據:
[ { "Group": 1, "data": [ { "Group": 1, "Groupheader": "質量管理", "Leftimg": "", "Left": "", "Min": "", "Right": "", "Rightimg": "" }, { "Group": 1, "Groupheader": "", "Leftimg": "", "Left": "", "Min": "質量巡檢", "Right": "", "Rightimg": "" } ] }, { "Group": 2, "data": [ { "Group": 2, "Groupheader": "設備管理", "Leftimg": "", "Left": "", "Min": "", "Right": "", "Rightimg": "" }, { "Group": 2, "Groupheader": "", "Leftimg": "", "Left": "", "Min": "設備專業點檢", "Right": "", "Rightimg": "" }, { "Group": 2, "Groupheader": "", "Leftimg": "", "Left": "", "Min": "設備日檢", "Right": "", "Rightimg": "" }, { "Group": 2, "Groupheader": "", "Leftimg": "", "Left": "", "Min": "設備周檢", "Right": "", "Rightimg": "" }, { "Group": 2, "Groupheader": "", "Leftimg": "", "Left": "", "Min": "設備月檢", "Right": "", "Rightimg": "" } ] } ]
以上是“js如何根據json數據中的某一個屬性來給數據分組”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。