How to convert array of objects to objects as required

convert array to object javascript
convert array of objects to object javascript
convert array to object php
convert array to object javascript es6
convert array to object javascript lodash
convert array to object jquery
map array to object javascript
convert array of string to array of objects javascript

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.

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

How do I convert array of Objects into one Object in JavaScript , You're probably looking for something like this: // original var arr = [ {key : '11', value : '1100', $$hashKey : '00X' }, {key : '22', value : '2200', $$hashKey : '018' }� Convert Object to Array Example. This section will see how we can convert Object to Array in Angular and Typescript with examples. Typescript Code. Let us assume that you have an object declared multiple properties. First Get the named keys using object.keys() method. This method retrieves keys from the given object and returns an array of keys.

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; }

Rewriting JavaScript: Converting an Array of Objects to an Object , For this problem lets assume you have been given the peopleArray and an id of the user and need to select them from the peopleArray. Below is� Use this policy template to convert an array of JSON objects in the body to a single JSON object. To start, specify the path to the parent object or collection and the property subpath within the parent where the array is located. Next, specify a property within each item of the array as the object key.

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);

Javascript Array to Object: How to convert an array into an object in , To convert an array into an object we will create a function and give it 2 properties, an array and a key. const convertArrayToObject = (array, key) => {}; We will then reduce the array, and create a unique property for each item based on the key we have passed in. To convert an array into an object we will create a function and give it 2 properties, an array and a key. const convertArrayToObject = ( array , key ) => { } ; We will then reduce the array, and create a unique property for each item based on the key we have passed in.

Convert an Array to an Object in JavaScript, Here is a need to convert an array into an object. values of all enumerable own properties from source objects(one or more) to a target object. The snippet is quite simple, an array of people objects where each object contains id, name, and age. For this problem lets assume you have been given the peopleArray and an id of the user and

How to convert an Object {} to an Array [] of key-value pairs in , Introduction: Objects, in JavaScript, is it's most important data-type and forms the building blocks for modern JavaScript. These objects are quite� How do I convert array of Objects into one Object in JavaScript? Ask Question Thanks, wrapping this in an object actually is not required. I have fixed it.

Object.fromEntries(), With Object.fromEntries , you can convert from Array to Object : and array manipulation methods, you are able to transform objects like this: This is a bit confusing because arrays are objects already, but I guess the point of the question is converting the array exotic object to an ordinary object. – Oriol Apr 3 '16 at 17:25 1

Comments
  • It is throwing bug can you please give a running example
  • Would you tell me which error, please ? Maybe object destructuring ?
  • 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.
  • 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.