tengo un regreso como este
[ { "tripw3": "1" }, { "tripw1": "2" }, { "tripw1": "3" }, { "tripw2": "4" }, { "tripw2": "5" }, { "tripw3": "6" }, { "tripw3": "7" }]y quiero que el resultado anterior sea
[ { tripw3: ["1", "6", "7"], tripw2: ["4", "5"], tripw1: ["3", "2"] }]Hasta ahora todavía estoy confundido para que esto suceda.
Puede obtener fácilmente el resultado usando reduce , Object.keys y forEach
const arr = [ { tripw4: "1", }, { tripw4: "2", }, { tripw4: "3", }, { tripw4: "4", }, { tripw4: "5", }, { tripw4: "6", }, { tripw4: "7", }, ]; const result = [arr.reduce((acc, curr) => { Object.keys(curr).forEach((k) => acc[k] ? acc[k].push(curr[k]) : (acc[k] = [curr[k]])); return acc; }, {})]; console.log(result); /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }