1

i have array of object transaction join with product and user table, i want to combine id with same value so it can display two different data in 1 object

Here's my data

let test = [
  {
            TransactionId: 1, //transaction table
            username: "A", //user table
            qty: 3, //product table
            product_name: "Logitech G733",
            price: $100,
            description: "Lalalalala",
            productId: 10
},
{


            TransactionId: 2,
            username: "B",
            qty: 1,
            product_name: "Razer",
            price: $200,
            description: "Blalalala",
            productId: 12
},
{
            TransactionId: 1,
            username: "A",
            qty: 1,
            product_name: "Mousepad",
            price: $50,
            description: "This is mousepad",
            productId: 7
},
{
            TransactionId: 3,
            username: "C",
            qty: 2,
            product_name: "Headphone",
            price: $300,
            description: "This is Headphone",
            productId: 2
},
]

this is the output i want

let test = [
  {
            TransactionId: 1,
            username: "A",
            qty: [3, 1],
            product_name: ["Logitech G733", "Mousepad"],
            price: [$100, $50]
            description: ["Lalalalala", "This is mousepad"],
            productId: [10, 7]
},
{


            TransactionId: 2,
            username: "B",
            qty: 1,
            product_name: "Razer",
            price: $200,
            description: "Blalalala",
            productId: 12
},
{
            TransactionId: 3,
            username: "C",
            qty: 2,
            product_name: "Headphone",
            price: $300,
            description: "This is Headphone",
            productId: 2
},
]

i tried using reduce and Object.assign but the output only shows object with "Mousepad" not make array ["logitech G733","Mousepad"]

5
  • $300 is not a valid JavaScript literal. Commented Dec 17, 2022 at 20:21
  • Is the username functionally dependent on the transactionId? Commented Dec 17, 2022 at 20:23
  • You've asked a question before. Any reason why you didn't give any feedback to the answers that were posted? Commented Dec 17, 2022 at 20:28
  • no its otherwise, i have sequelize model User.hasMany(Transaction) and Transaction.belongsTo(User). i'm new to node.js sorry if my explanations wrong Commented Dec 18, 2022 at 7:33
  • oh.. back then i'm new in stackoverflow and i can't give upvote because my reputation score is lower than 10 or 15 or minimum requirement to give upvote Commented Dec 18, 2022 at 7:35

1 Answer 1

1

there should be many ways to do it, I used a map to combine the transactions and you can do whatever you need for the map after. For example

const test = [
  {
              TransactionId: 1, //transaction table
              username: "A", //user table
              qty: 3, //product table
              product_name: "Logitech G733",
              price: 100,
              description: "Lalalalala",
              productId: 10
  },
  {


              TransactionId: 2,
              username: "B",
              qty: 1,
              product_name: "Razer",
              price: 200,
              description: "Blalalala",
              productId: 12
  },
  {
              TransactionId: 1,
              username: "A",
              qty: 1,
              product_name: "Mousepad",
              price: 50,
              description: "This is mousepad",
              productId: 7
  },
  {
              TransactionId: 3,
              username: "C",
              qty: 2,
              product_name: "Headphone",
              price: 300,
              description: "This is Headphone",
              productId: 2
  },
]

const tMap = new Map();

test.forEach(transation => {
  tMap.set(transation.TransactionId, { ...tMap.get(transation.TransactionId), ...transation });
})

If you wan to deep combined you can use some tool like lodash deep merge

tMap.set(transation.TransactionId, _.merge(tMap.get(transation.TransactionId), transation))

Then you have a map based on your tranastion Id, and you can decide what to do next. If you wan the array back you can simple run

console.log(Array.from(tMap.values()));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.