How to group and count elements of a JSON file with multiple objects on javascript?

I am very new to javascript, and so far everything was fine until I ran into this dilemma, I need to restructure a JSON file as follows:

{
    firstName: "Joel",
    lastName: "Munz",
    total: 154,
    transaction: "111111",
    articles:[
        {
            sku: "abc123456",
            number: 2,
            price: 65
        },
        {
            sku: "def123456",
            number: 2,
            price: 24
        }
    ]
},
{
    firstName: "Patel",
    lastName: "Dass",
    total: 85,
    transaction: "222222",
    articles: [
        {
            sku: "abc123456",
            number: 1,
            price: 65
        },
        {
            sku: "def123456",
            number: 2,
            price: 10
        }
    ]
}

And my source file has the following structure:

{
    "transactions":[
        {
            "items":[
                {
                    "price":65,
                    "sku":"abc123456"
                },
                {
                    "price":65,
                    "sku":"abc123456"
                },
                {
                    "price":12,
                    "sku":"def123456"
                },
                {
                    "price":12,
                    "sku":"def123456"
                }
            ],
            "total":154,
            "transaction":"111111",
            "customer":{
                "firstName":"Joel",
                "lastName":"Munz"
            }
        },
        {
            "items":[
                {
                    "price":65,
                    "sku":"abc123456"
                },
                {
                    "price":10,
                    "sku":"ghi123456"
                },
                {
                    "price":10,
                    "sku":"ghi123456"
                }
            ],
            "total":85,
            "transaction":"222222",
            "customer":{
                "firstName":"Patel",
                "lastName":"Dass"
            }
        }
    ]
}

what I have tried is the following, but it doesn’t have the structure that I need. I’ve read about using the reduce () forEach () function, but I don’t know how to implement it to match the format I need.

const trans = data.transactions.map(transaction => ({
    "firstName": transaction.customer.firstName,
    "lastName": transaction.customer.lastName,
    "total":transaction.total,
    "order_id": transaction.transaction,  //OP
    "articles": {
        "sku":transaction.items.map(Item => Item.sku),
        "number":transaction.items.map(Item => Item.sku).length,
        "price":transaction.items.map(Item => Item.price)}
    }))
console.log(trans);

Any suggestions?
Thanks for the help!

89 thoughts on “How to group and count elements of a JSON file with multiple objects on javascript?”

  1. You’re extremely close, it’s just that instead of mapping each property of the articles property, you want to map the entire transaction.items array and assign the returned array to articles.

    "articles": transaction.items.map(Item => ({
        "sku": Item.sku,
        "number": Item.sku.length, //unsure what you want this value to be; index?
        "price": Item.price
      }))
    

    instead of:

    "articles": {
      "sku": transaction.items.map(Item => Item.sku),
      "number": transaction.items.map(Item => Item.sku).length,
      "price": transaction.items.map(Item => Item.price)}
    
    const data = { "transactions": [{ "items": [{ "price": 65, "sku": "abc123456" }, { "price": 65, "sku": "abc123456" }, { "price": 12, "sku": "def123456" }, { "price": 12, "sku": "def123456" }], "total": 154, "transaction": "111111", "customer": { "firstName": "Joel", "lastName": "Munz" } }, { "items": [{ "price": 65, "sku": "abc123456" }, { "price": 10, "sku": "ghi123456" }, { "price": 10, "sku": "ghi123456" }], "total": 85, "transaction": "222222", "customer": { "firstName": "Patel", "lastName": "Dass" } }] };
    
    const trans = data.transactions.map(transaction => ({
      "firstName": transaction.customer.firstName,
      "lastName": transaction.customer.lastName,
      "total": transaction.total,
      "order_id": transaction.transaction,  //OP
      "articles": transaction.items.map((Item, index) => ({
        "sku": Item.sku,
        "number": index+1, // guessing you want sequential numbers?
        "price": Item.price
      }))
    }))
    
    console.log(JSON.stringify(trans, null, 2));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Reply

Leave a Comment