打印值:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>测试</title> </head> <body> </body> <script type="text/javascript"> var res = [ [{ "key": "颜色", "value": "黑色" }, { "key": "容量", "value": "32G" } ], [{ "key": "颜色", "value": "黑色" }, { "key": "容量", "value": "64G" } ], [{ "key": "颜色", "value": "白色" }, { "key": "容量", "value": "32G" } ], [{ "key": "颜色", "value": "白色" }, { "key": "容量", "value": "64G" } ], ]; var arr1 = []; // 存放所有key(去重后的) var arr2 = []; // 组合成想要的格式 for (var i = 0; i < res.length; i++) { for (var j = 0; j < res[i].length; j++) { if (!arr1.includes(res[i][j].key)) { arr1.push(res[i][j].key); } } } for (var i = 0; i < arr1.length; i++) { var obj = { "key": arr1[i], "value": [] }; arr2.push(obj); } // 赋值 for (var i = 0; i < res.length; i++) { for (var j = 0; j < res[i].length; j++) { for (var k = 0; k < arr2.length; k++) { if (res[i][j].key == arr2[k].key) { if (!arr2[k].value.includes(res[i][j].value)) { arr2[k].value.push(res[i][j].value); } } } } } console.log(res,'整体数据'); console.log(arr2,'处理后的数据'); </script> </html>