array with [1, 2], is there an elegant way to merge it so it's just {x:1, y:2}?
{x: 'x', y: 'y'} you mean ?
You want to use the keys of an object to fill in the array values? 🤔
let obj = {x: '', y: ''} let arr = [1, 2] Object.keys(obj).reduce((newObj, item, i) => { newObj[item] = arr[i] return newObj }, {})
I want to fill in X amount of keys depending on how many elements are in the array
That looks about right lol
I noticed it crashes after changing the object size, so I made it arr[i] || obj[item] and it's working good 👌
const [x, y] = [1, 2]; const obj = {x, y};
And if they're not exactly the same size? For example {x, y, z:3} and [1, 2] for {x:1, y:2, z:3}?
I'm not entirely sure what you're trying to do.
Here's a few examples. {x} • [1, 2] = {x:1} {x, y} • [1, 2] = {x:1, y:2} {x, y, z} • [1, 2] = {x:1, y:2, z}
Just pop the values in one by one until you can't fit anymore into either one or until it's done lol but the solution here worked
Обсуждают сегодня