Tengo una matriz de objetos que tienen varios valores, pero tengo que filtrar los valores en función de las claves en una matriz y también fusionar el valor con la matriz que tiene el mismo valor. Lo he intentado en diferentes pasos, ¿se puede hacer en un solo mapa/filtro/reducir?
const a = [ {abc: ['add', 'edit'], cond: []}, {cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: []}, {cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work']}, ]; const expected result = { cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx', 'delete_matx'], work: ['add_work', 'edit_work','view_work', 'delete_work'] } const b = a.map(el => el.cond); const c = a.map(el => el.matx); const d = a.map(el => el.work); result = {cond: [...b], matx: [...c], work: [...d]}Puede recopilar todos los valores de las propiedades deseadas en un conjunto y crear un nuevo objeto a partir de los pares clave/valor.
const data = [{ abc: ['add', 'edit'], cond: [] }, { cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: [] }, { cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work'] }], keys = ['cond', 'matx', 'work'], addToSet = (s, v) => s.add(v), result = Object.fromEntries(keys.map(k => [ k, [...data.reduce((r, o) => (o[k] || []).reduce(addToSet, r), new Set)] ])); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; }Puede lograr esto usando un solo método de reduce . Todas las claves se evalúan dinámicamente, por lo que no importa cuántas claves agregue/elimine a sus objetos, obtendrá claves agregadas dinámicamente:
const a = [ {abc: ['add', 'edit'], cond: []}, {cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: []}, {cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work']}, ]; const result = a.reduce((prev, cur) => { // get all keys const keys = Object.keys(cur); // add each key and related values one by one keys.forEach(el => { if(prev[el] === undefined) prev[el] = [] prev[el].push(cur[el]) // remove duplicates prev[el] = prev[el].flat() }) return prev }, {}) console.log(result)Se puede hacer con forEach ... .map() y .filter() siempre devolverán una matriz, no un objeto.
const a = [ {abc: ['add', 'edit'], cond: []}, {cond: ['add_cond', 'edit_cond', 'view_cond'], matx: ['add_matx', 'edit_matx', 'delete_matx'], work: ['add_work', 'edit_work'], def: []}, {cond: ['add_cond', 'edit_cond', 'view_cond', 'delete_cond'], matx: ['add_matx', 'edit_matx', 'view_matx', 'store_matx'], work: ['view_work', 'delete_work']}, ]; const result = {cond: [], work: [], matx: []} a.forEach(x=> { x.cond && result.cond.push(...x.cond); x.work && result.work.push(...x.work); x.matx && result.work.push(...x.matx); } ); console.log(result);