const arrayValues = ["blue", 1, "red", 4, "yellow", 2, "green", 7]
const arrayObjects = []
for (let i = 0; i < arrayValues.length; i += 2) {
const newObject = {}
const key = arrayValues[i]
const value = arrayValues[i+1]
newObject[key] = values
arrayObjects.push(newObject)
}
// Output:
[
{
"blue": 1
},
{
"red": 4
},
{
"yellow": 2
},
{
"green": 7
}
]
References: https://stackoverflow.com/questions/42017674/convert-array-into-key-value-pairs