Reputation: 1841
Here is the code:
let bucket = [[],[],[],[],[],[],[],[],[],[]];
bucket = {...Object.keys(bucket)
.sort((a,b) => b-a)
.filter(key => key > 0)
.map(key => '-'+key),
...bucket};
console.log(bucket);
Problem: the first line of code is not adding the negative keys of the original bucket object into the object, with all properties (keys) having empty arrays as their corresponding value.
Bucket only shows its original properties and values after this line of code is evaluated
How can I get this to work?
Upvotes: 0
Views: 87
Reputation: 191936
The Array.map()
creates an array of strings with your negative keys, and they are overwritten when you spread the bucket
array. Instead create pairs of [new key, []]
convert to an object with Object.fromEntries()
, and then spread them:
const bucket = [[],[],[],[],[],[],[],[],[],[]];
const result = {
...Object.fromEntries(
Object.keys(bucket)
.filter(key => key > 0)
.sort((a, b) => b-a)
.map(key => [-key, []])
),
...bucket};
console.log(result);
Upvotes: 1