-2

How can I iterate an array and take first five value as key of Object and all the other values as Object value. For an example : I have an array Like this

["name", "time", "date", "address", "Balance", "George", "Nancy", "Shivam", "George", "10:00AM", "12:00PM", "4:00PM", "", "10/10/2021", "05/20/2021", "06/05/2019", "07/10/2021" ,"308 E Parker St", "505 10th Ave", "2706 Pershing Blvd" , "988 Cannon Farm Rd", "546.00", "648.00", "899.99", ""]

I want something like this:

[{name : "George", time : "10:00AM", date: "10/10/2021", address :308 E Parker St", Balance : "546.00" }, {name : "Nancy", time : "12:00PM", date: "05/20/2021", address: "505 10th Ave", Balance : "648.00" }, {name : "Shivam", time : "4:00PM", date: "06/05/2019", address: "2706 Pershing Blvd", Balance : "899.99" }, {name : "George", time : "", date: "07/10/2021", address: "988 Cannon Farm Rd", Balance : "" } ]

how I can separate the name, time date and others values from the given array since the values are dynamic.

1 Answers1

-1

Alif, You can use Array.reduce this may help - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce and you can take a look at this as well - How to loop through array of objects and make key value pairs?

  • 1
    Thanks for your quick reply. I really appreciate that. The problem here is, Values are dynamic . It'll change dynamically. What I needed is to Make the first five value as Key and loop through the full array and put the values accordingly to each key. I don't know how I can separate the name, time date and others values . – Alif Hossain Oct 20 '21 at 16:38