I’m having a little bit of trouble creating an object of a couple of fields, pushing them and creating a nice looking object:
// function to extract integer from key field...
function pad3(position) {
return parseInt((position < 10 ? position.charAt(1) : position))
}
for (let field of formData) {
key = field[0]
val = field[1]
position = pad3(key.substring(0,2)) // 00- becomes 0, 01- becomes 1 etc...
let obj = []
obj = {[key.substring(3,key.length)]: val}
arr.push(obj)
}
Console logging key and val within this loop during iteration looks like:
console.log(key) // 00-account_a
console.log(val) // 111
console.log(key) // 00-account_b
console.log(val) // 222
console.log(key) // 01-account_a
console.log(val) // 333
console.log(key) // 01-account_b
console.log(val) // 444
console.log(key) // 02-account_a
console.log(val) // 555
console.log(key) // 02-account_b
console.log(val) // 666
and so on …
I would like to create a nice looking Array object from this data but I’m struggling…
How do I create a nice looking array object based on the position variable?
Something Like:
Accounts [
[0] { // <-- [0] based on "position" variable ...
account_a: 111,
account_b:222
},
[1] {
account_a: 333,
account_b:444
},
[2] {
account_a: 555,
account_b:666
},
]