tengo una matriz como esta
data = [ { name : "sa", attributes : [{skin : "green"},{nose:"good"}]}, { name : "sa", attributes : [{skin : "red"},{nose:"bad"}]}, { name : "sa", attributes : [{skin : "green"},{nose:"good"}]}, { name : "sa", attributes : [{nose:"good}]}, ]entonces quiero la agrupación de la matriz basada en el atributo de la piel. Estoy usando una biblioteca de guiones bajos como esta
const attributeType = "skin"; const groupedCollections = _.groupBy(colls, (col) => { const data = col.attributes.find((attribute) => { const keys = Object.keys(attribute); return keys.indexOf(attributeType) > -1 ? true : false }); return data?.value }); pero esto está agrupando bajo undefined .
¿Alguna ayuda?
Debe devolver el valor de la propiedad deseada.
const colls = [{ name: "sa", attributes: [{ skin: "green" }, { nose: "good" }] }, { name: "sa", attributes: [{ skin: "red" }, { nose: "bad" }] }, { name: "sa", attributes: [{ skin: "green" }, { nose: "good" }] }, { name: "sa", attributes: [{ nose: "good" }] }], attributeType = "skin", groupedCollections = _.groupBy(colls, ({ attributes }) => attributes .find(attribute => attributeType in attribute) ?.[attributeType] ); console.log(groupedCollections); .as-console-wrapper { max-height: 100% !important; top: 0; } <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>