0

I need to create multiple objects where I have one array of keys to go to multiple different sets of values. I have gotten to the point where I can set the keys to the correct data, but I don't know how to assign each set of key/value pairs to an object. I would need something like object(i) for each object created

function toJSON() {

let temp = {};
let temp2 = [];
let temp3 = [];
for (let i = 0; i < values.length; i++) {

    for (let j = 0; j < key.length; j++) {
        temp = {
            [key[j]]: values[i][j],
        }
        temp2.push(temp)

    }
    //Then take this info and put it into an object
}


console.log(temp2)

}

2
  • I think Object.values(), Object.keys(), Object.entries() will be handy to you. Commented Nov 16, 2020 at 0:07
  • To assign porperties to an object: obj[property] = value; Commented Nov 16, 2020 at 0:08

1 Answer 1

1

I'll assign some arbitrary data to keys and values so you can verify that I've understood the question:

const keys = ['a', 'b', 'c']
const values = [[1,2,3], [4,5,6], [7,8,9]]

const result = values.map(value => Object.assign({}, ...keys.map((key, i) =>
   ({[key]: value[i]}))))
   
console.log(result)
   

Is that the output you were looking for?

Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I was looking for, time to learn the map function, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.