Tengo una gran variedad de objetos y estoy tratando de consolidar donde tengo una matriz de objetos que se ve a continuación: cada artículo (por ejemplo, plátano) estará en 2 objetos separados mientras intento agregar todas las órdenes de compra y venta pedidos, y estos son precios/datos diferentes.
He intentado tomar los emparejamientos únicos (plátano, manzana, naranja, etc.) y mapear a través de eso y dentro de otro mapa de los datos, pero no puedo resolverlo.
let uniquePairing = Array.from([...new Set(data.map(item => item["name"]))]) **let data = [{ name:'banana, price:( average cost) type:buy items: ( total bananas) fee: ( total fees) }, { name:'banana, price:( average cost) type:sell items: ( total bananas) fee: ( total fees) }, { apples... ]**ejemplo de datos
let data = [ { name:"banana", price:1, type: "buy", fee: 0.5, items:25 }, { name:"banana", price:1.2, type: "buy", fee: 0.5, items:25 }, { name:"banana", price:2, type: "sell", fee: 0.5, items:25 }, { name:"apple", price:1, type: "buy" fee: 0.5 items:25 }, { name:"apple", price:1.2, type: "buy", fee: 0.5, items:25 }, { name:"apple", price:2, type: "sell", fee: 0.5, items:25 } ]Si entiendo su pregunta correctamente, puede usar la función de reduce :
let uniquePairing = data.reduce((total, currentObj) => { const name = currentObj.name, type = currentObj.type; // Group by both `type` and `name` let relatedObj = total.find(obj => obj.name === name && obj.type === type); if (!relatedObj) { total.push({ ...currentObj, // Adding `totalPrice` and `objCount` to track the cost average totalPrice: currentObj.price, objCount: 1 }) } else { relatedObj.totalPrice += currentObj.price; relatedObj.objCount++; relatedObj.price = relatedObj.totalPrice / relatedObj.objCount; relatedObj.items += currentObj.items; relatedObj.fee += currentObj.fee; } return total; }, []);Podrías hacerlo así:
let data = [ { name:"banana", price:1, type: "buy", fee: 0.5, items:25 }, { name:"banana", price:1.2, type: "buy", fee: 0.5, items:25 }, { name:"banana", price:2, type: "sell", fee: 0.5, items:25 }, { name:"apple", price:1, type: "buy", fee: 0.5, items:25 }, { name:"apple", price:1.2, type: "buy", fee: 0.5, items:25 }, { name:"apple", price:2, type: "sell", fee: 0.5, items:25 } ] const getFruitData = (fruits, fruit) => fruits.filter(fr=> fr.name===fruit.name && fr.type=== fruit.type) const getTotal = (fruits, field) => fruits.reduce((acc,cur)=> acc+=cur[field], 0) const isRegistered = (fruits, value) => fruits.some(fruit=> fruit.type === value.type && fruit.name===value.name) const getFruits = (fruits) => { return fruits.reduce((acc, cur)=> { if(isRegistered(acc, cur)){ return acc } acc.push({name: cur.name, type: cur.type}) return acc }, []) } function mergeData(arr){ const fruits = getFruits(arr) return fruits.map(fruit=> { const data = getFruitData(arr, fruit) return { name: fruit.name, price: getTotal(data, "price"), type: fruit.type, fee: getTotal(data, "fee"), items: getTotal(data, "items") } }) } console.log(mergeData(data))