简体   繁体   中英

Iterate an Array and make Object Key value pairs

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.

Alif,您可以使用 Array.reduce 这可能会有所帮助 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce你也可以看看这个 - 如何遍历对象数组并制作键值对?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM