-1

I want to dynamically specify negative values as seen in this exact question.

Rules for unquoted JavaScript Object Literal Keys?

I want to also accomplish this using some method of dynamic bracket notation, using a string equivalent of a negative remainder (modulus operation).

bucketObj[key] reports undefined, since I have not pre set a key to store a corresponding value. How do I dynamically set a key of an object in either bracket notation, dot notation, or both?

 function toDigitBuckets(array, radix) {
    let bucket = JSON.parse(JSON.stringify([...Array(radix).fill([])]));
    let bucketObj = Object.assign(bucket);
    array.forEach(val => { let key = String.toString(val % 10); bucketObj[key].push(val) });
    return bucket; 
  }
  
  let array = [-7, -49, 84, 39, -31, 95, 7, -8, -13, -32, 93, 40, -81, -30, -57, -57, 49, 66, -64, 42, 35, 29, -57, 41, 93, 34, -45, -15, 51, 16, 97, -88, 52, -69, 56, -16, -91, 51, 10, -21, 80, 78, -5, 18, -20, -98, 72, -94, 11, -83, -31, 13, -21, 39, -47, 8, -98, 95, 52, -18, 77, -11, -38, -46, -98, 48, -45, -4, 76, -32, -81, 67, -82, 9, -60, -20, 0, 33, -12, 77, 65, 45, -22, 99, -47, -83, -81, 10, -99, 16, 23, 5, -57, 89, -62, 9, -16, 79, 5, -2];
  
  let radix = 10;
  console.log(JSON.stringify(toDigitBuckets(array, radix)));
Vahe
  • 1,699
  • 3
  • 25
  • 76
  • I hope I have not inadvertently hinted an answer to my question, but I suspect my brain was giving me clues that might help. An answer would be helpful still for vote consideration. – Vahe Sep 19 '21 at 20:19
  • 1
    please add the wanted result. – Nina Scholz Sep 19 '21 at 20:26
  • Something like `array.forEach(val => { key = String.toString(val % 10); {...,[key]:val }});` – Vahe Sep 19 '21 at 20:29
  • Please suggest if my is just the object label, if so that is really cool – Vahe Sep 19 '21 at 20:32
  • 1
    i have no idea. please add the result in literal notation. – Nina Scholz Sep 19 '21 at 20:35
  • This is the most concise I got, no syntax underlines show up : `array.forEach(val => { return {...bucket, [key]:val}});` and the Object Literal is `{...bucket, [key]:val}` where bucket is the object to have a new key added dynamically – Vahe Sep 19 '21 at 20:36
  • The answer above does not accomplish the desired result because, from what I know forEach does not return a value. My answer is close, but I need to find an alternate close version of the way I chose. – Vahe Sep 20 '21 at 10:12
  • I have provided a check mark to accept, I was a little out of sync yesterday, thanks for the reminder. Thank you. – Vahe Sep 20 '21 at 20:43

1 Answers1

1

You can use Array#reduce with an object as the accumulator. Each time a new key is encountered, first set that property value to be an empty array.

function toDigitBuckets(array, radix) {
  return array.reduce((acc, curr) => 
    ((acc[curr % radix] ??= []).push(curr), acc), {});
}
let array = [-7, -49, 84, 39, -31, 95, 7, -8, -13, -32, 93, 40, -81, -30, -57, -57, 49, 66, -64, 42, 35, 29, -57, 41, 93, 34, -45, -15, 51, 16, 97, -88, 52, -69, 56, -16, -91, 51, 10, -21, 80, 78, -5, 18, -20, -98, 72, -94, 11, -83, -31, 13, -21, 39, -47, 8, -98, 95, 52, -18, 77, -11, -38, -46, -98, 48, -45, -4, 76, -32, -81, 67, -82, 9, -60, -20, 0, 33, -12, 77, 65, 45, -22, 99, -47, -83, -81, 10, -99, 16, 23, 5, -57, 89, -62, 9, -16, 79, 5, -2];
let radix = 10;
console.log(toDigitBuckets(array, radix));
.as-console-wrapper{max-height:100%!important;top:0}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80