0

I have two array of objects from which I am trying to loop and get a new object as per requirement.

My 1st array

This the header one

let billHeader = [
      {
        "billno": "A1",
        "companyid": "AAAA",
        "pending": "Y",
        "cancelled": "N",
        "salesman": "admin",
        "netamount": "225.0000",
        "billdate": "2020-01-16",
        "billtime": "11:15:29"
      },
      {
        "billno": "A2",
        "companyid": "AAAA",
        "pending": "Y",
        "cancelled": "N",
        "salesman": "admin",
        "netamount": "1500.0000",
        "billdate": "2020-01-16",
        "billtime": "11:18:29"
      }
    ]

second one is buillInfo

let billInfo =  [
  {
    "billno": "A1",
    "itemcode": "1002",
    "companyid": "AAAA",
    "unitprice": "125.0000",
    "itemname": "MANCHOW NV SOUP",
    "quantity": "1.0000",
    "totalamount": "125.0000",
    "categoryname": "SOUP"
  },
  {
    "billno": "A1",
    "itemcode": "1001",
    "companyid": "AAAA",
    "unitprice": "100.0000",
    "itemname": "MANCHOW V SOUP",
    "quantity": "1.0000",
    "totalamount": "100.0000",
    "categoryname": "SOUP"
  },
  {
    "billno": "A2",
    "itemcode": "1001",
    "companyid": "AAAA",
    "unitprice": "300.0000",
    "itemname": "MANCHOW V SOUP",
    "quantity": "2.0000",
    "totalamount": "600.0000",
    "categoryname": "SOUP"
  },
  {
    "billno": "A2",
    "itemcode": "1003",
    "companyid": "AAAA",
    "unitprice": "300.0000",
    "itemname": "Name Item",
    "quantity": "3.0000",
    "totalamount": "6300.0000",
    "categoryname": "SOUP"
  }
]

I want to loop through both of them and have an output exactly like this

 {
  "module": "outletbills",
  "status": "success",
  "billinfo": [
    {
      "billno": "A1",
      "companyid": "AAAA",
      "pending": "N",
      "cancelled": "N",
      "salesman": "admin",
      "netamount": "225.0000",
      "billdate": "2020-01-16",
      "billtime": "11:15:29",
      "billitems": [
        {
          "billno": "A1",
          "itemcode": "1001",
          "companyid": "AAAB",
          "unitprice": "100.0000",
          "itemname": "MANCHOW  V SOUP",
          "quantity": "1.0000",
          "totalamount": "100.0000",
          "categoryname": "SOUP"
        },
        {
          "billno": "A498",
          "itemcode": "1002",
          "companyid": "AAAB",
          "unitprice": "125.0000",
          "itemname": "MANCHOW NV SOUP",
          "quantity": "1.0000",
          "totalamount": "125.0000",
          "categoryname": "SOUP"
        }
      ]
    },
    {
      "billno": "A2",
      "companyid": "AAAA",
      "pending": "N",
      "cancelled": "N",
      "salesman": "admin",
      "netamount": "1500.0000",
      "billdate": "2020-01-16",
      "billtime": "11:16:41",
      "billitems": [
        {
          "billno": "A2",
          "itemcode": "1001",
          "companyid": "AAAB",
          "unitprice": "900.0000",
          "itemname": "MANCHOW  V SOUP",
          "quantity": "2.0000",
          "totalamount": "200.0000",
          "categoryname": "SOUP"
        },
        {
          "billno": "A2",
          "itemcode": "1002",
          "companyid": "AAAA",
          "unitprice": "125.0000",
          "itemname": "MANCHOW NV SOUP",
          "quantity": "1.0000",
          "totalamount": "600.0000",
          "unitcode": "NOS"
        }
      ]
    }
  ]
}

How to start? I know several methods which will help me out like map, reduce, filter but don't know how to exactly do this.

3 Answers 3

1

Bill items relate to a bill by its bill number - billno

return {
    "module": "outletbills",
    "status": "success",
    "billinfo": billHeader.map(billHeaderEntry => {
        return {
            ...bill,
            billitems: billInfo.filter(billInfoEntry =>
                billHeaderEntry.billno === billInfoEntry.billno)
        }
    }),
}

And if you care not to modify some state...

for (let billHeaderEntry of billHeader) {
    billHeader.billItems = billInfo.filter(billInfoEntry =>
        billHeaderEntry.billno === billInfoEntry.billno)
}

return billHeader
Sign up to request clarification or add additional context in comments.

2 Comments

It is throwing bug can you please give a running example
Would you tell me which error, please ? Maybe object destructuring ?
1

You could take a hash table for same groups and assign the items to the belonging group.

var billHeader = [{ billno: "A1", companyid: "AAAA", pending: "Y", cancelled: "N", salesman: "admin", netamount: "225.0000", billdate: "2020-01-16", billtime: "11:15:29" }, { billno: "A2", companyid: "AAAA", pending: "Y", cancelled: "N", salesman: "admin", netamount: "1500.0000", billdate: "2020-01-16", billtime: "11:18:29" }],
    billInfo = [{ billno: "A1", itemcode: "1002", companyid: "AAAA", unitprice: "125.0000", itemname: "MANCHOW NV SOUP", quantity: "1.0000", totalamount: "125.0000", categoryname: "SOUP" }, { billno: "A1", itemcode: "1001", companyid: "AAAA", unitprice: "100.0000", itemname: "MANCHOW V SOUP", quantity: "1.0000", totalamount: "100.0000", categoryname: "SOUP" }, { billno: "A2", itemcode: "1001", companyid: "AAAA", unitprice: "300.0000", itemname: "MANCHOW V SOUP", quantity: "2.0000", totalamount: "600.0000", categoryname: "SOUP" }, { billno: "A2", itemcode: "1003", companyid: "AAAA", unitprice: "300.0000", itemname: "Name Item", quantity: "3.0000", totalamount: "6300.0000", categoryname: "SOUP" }],
    groups = {},
    result = billHeader.map(o => ({ ...o, billitems: groups[o.billno] = [] }));

billInfo.forEach(o => groups[o.billno].push(o));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

4 Comments

hey there is bug in your code it is giving me only one bill data i.e for A1 it is not outputting A2
i get both. do you use the same data?
No data is dynamic, here I am having two bills A1 and A2, In future I can have A1,A2,A3 many more
@manishthakur, this is dynamic as well by taking billno as key for a group. please state what not work with your data.
0

Here is the naive approach to solve this. I think it's much readable and it'll provide you a better idea about what is going. You can easily loop through each arrays and add the new billitems to your billHeader. You can add your thought here to make it shorter.

var output = {
  "module": "outletbills",
  "status": "success"
};
var billHeader = [{ billno: "A1", companyid: "AAAA", pending: "Y", cancelled: "N", salesman: "admin", netamount: "225.0000", billdate: "2020-01-16", billtime: "11:15:29" }, { billno: "A2", companyid: "AAAA", pending: "Y", cancelled: "N", salesman: "admin", netamount: "1500.0000", billdate: "2020-01-16", billtime: "11:18:29" }],
billInfo = [{ billno: "A1", itemcode: "1002", companyid: "AAAA", unitprice: "125.0000", itemname: "MANCHOW NV SOUP", quantity: "1.0000", totalamount: "125.0000", categoryname: "SOUP" }, { billno: "A1", itemcode: "1001", companyid: "AAAA", unitprice: "100.0000", itemname: "MANCHOW V SOUP", quantity: "1.0000", totalamount: "100.0000", categoryname: "SOUP" }, { billno: "A2", itemcode: "1001", companyid: "AAAA", unitprice: "300.0000", itemname: "MANCHOW V SOUP", quantity: "2.0000", totalamount: "600.0000", categoryname: "SOUP" }, { billno: "A2", itemcode: "1003", companyid: "AAAA", unitprice: "300.0000", itemname: "Name Item", quantity: "3.0000", totalamount: "6300.0000", categoryname: "SOUP" }],
groups = {},
result = billHeader.map(o => ({ ...o, billitems: groups[o.billno] = [] }));

billInfo.forEach(o => groups[o.billno].push(o));

output.billinfo = result;

console.log(output);

2 Comments

hey it is giving array of objects, I want just object please check my output I have suggested
@manishthakur i've returned the billinfo array, Please check now i've edited the code and now it's returning the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.