Estoy intentando agrupar elementos de una matriz (que contienen detalles de pedidos). aquí está mi estructura de matriz:
[{"id":"myid","base":{"brands":["KI", "SA"],"country":"BG","status":"new"}},{"id":"DEC-00500331","base":{"brands":["DEC"],"country":"UK","status":"new"},"psp":{"name":"adyen","status":"paid"}}]Un pedido está relacionado con el sitio web de un país y puede contener una o más marcas. por ejemplo, en un pedido puedo tener un artículo de marca1 y un artículo de marca2.
Necesito agrupar estos pedidos, por país y marca para poder tener una matriz u objeto consolidado.
Puedo agrupar por país fácilmente:
let groupedDataByCountryAndBrand = _.groupBy(orders.value, 'base.country') Object.keys(groupedDataByCountryAndBrand).forEach(key => { table.push( { country : key, //to be reviewd : for the two brands or more in one order new : groupedDataByCountryAndBrand[key].filter( order => (order.base.status === SFCC_STATUS.new || SFCC_STATUS.open || SFCC_STATUS.completed )).length }) })Aquí está el resultado:
Desafortunadamente esto no está funcionando para mí. Necesito agrupar los pedidos por país y marca para poder contar los pedidos recién creados para cada marca por país.
El resultado que espero es algo como esto:
{ "country" : "FR", "brand": "adidas", "pending": 4 "new" : 3, "an other status": 5 }¿Tienes alguna idea de cómo puedo lograr esto?
Estoy usando lodash con el componente vue. Gracias.
Esto es lo que está buscando, sin usar bibliotecas adicionales:
const data = [{"id":"myid","base":{"orderNumber":"0500332","marketPlaceOrderCode":"","creationDate":"2022-06-14T10:49:10Z","source":"sfcc","brands":["KI", "SA"],"country":"BG","status":"new","totalEuro":12,"currency":"BGN","units":1,"coupons":null,"shipping":{"id":"BG01","name":"Speedy COD","status":"not_shipped"}},"psp":{"name":"payu","method":null,"status":null},"tms":null},{"id":"DEC-00500331","base":{"orderNumber":"id2","marketPlaceOrderCode":"","creationDate":"2022-06-14T10:41:29Z","source":"sfcc","brands":["DEC"],"country":"UK","status":"new","totalEuro":57,"currency":"GBP","units":1,"coupons":null,"shipping":{"id":"ECA_DPD_ST","name":"Standard shipping","status":"not_shipped"}},"psp":{"name":"adyen","method":null,"status":"paid"},"tms":null}]; const rawResult = normalizeData(data); console.log(makeItReadable(rawResult)); function normalizeData(data) { const result = {}; data.forEach((order) => { const orderData = order.base; // add country to result (if not exists) if (!result[orderData.country]) { result[orderData.country] = {}; } orderData.brands.forEach((brand) => { // add brand to exact country (if not exists) if (!result[orderData.country][brand]) { result[orderData.country][brand] = {}; } // add status to exact brand and country (if not exists) if (!result[orderData.country][brand][orderData.status]) { result[orderData.country][brand][orderData.status] = 0; } // increment status count ++result[orderData.country][brand][orderData.status]; }); }); return result; } function makeItReadable(rawData) { const readableResult = []; Object.keys(rawData).map((country) => { Object.keys(rawData[country]).map((brand) => { readableResult.push({country, brand, ...rawData[country][brand]}); }); }); return readableResult; }Este código te dará el siguiente resultado:
[ { country: 'BG', brand: 'KI', new: 1 }, { country: 'BG', brand: 'SA', new: 1 }, { country: 'UK', brand: 'DEC', new: 1 } ]