Dinesh Beura
Dinesh Beura

Reputation: 171

How to convert an object to an array of specific format

{
    "vendorName": "RAKESH KUMAR",
    "vendorContact": "8876545678",
    "vendorPAN": "ATMPB6657F",
    "vendorBankDetails": [
        {
            "vendorAccount": "3456787654",
            "vendorBankCode": "AXIS123",
            "vendorBankName": "AXIS BANK"
        }
    ],
    "createdAt": "2021-09-01 00:18:10"
}

to

[0:{
    "vendorName": "RAKESH KUMAR",
    "vendorContact": "8876545678",
    "vendorPAN": "ATMPB6657F",
    "vendorBankDetails": [
        {
            "vendorAccount": "3456787654",
            "vendorBankCode": "AXIS123",
            "vendorBankName": "AXIS BANK"
        }
    ],
    "createdAt": "2021-09-01 00:18:10"
}]

Upvotes: 0

Views: 42

Answers (2)

Gultekin Ahmed
Gultekin Ahmed

Reputation: 78

let myObjects = [];

myObject.push({"vendorName":"RAKESH KUMAR","vendorContact":"8876545678","vendorPAN":"ATMPB6657F","vendorBankDetails":[{"vendorAccount":"3456787654","vendorBankCode":"AXIS123","vendorBankName":"AXIS BANK"}],"createdAt":"2021-09-01 00:18:10"});

console.log(myObject);

enter image description here

Upvotes: 0

Mureinik
Mureinik

Reputation: 311316

It seems you're just creating an array with a single element which is the aforementioned object. You can do this by surrounding the object with square brackets:

const myArray = [myObject];

Upvotes: 2

Related Questions