0

I have an array of objects (I don't know how many object, maybe 3, maybe 10) example:

const data = [
  {name: 'Jane', age: 25},
  {name:'Luke', age: 55},
  {name:'Martha', age: '16'},
  ...and so on
]

now I want to map through this array and create a key-value pair for each, and as a key I want to use a letter of alphabet (starting with c). expected result is:

{
  c:{name: 'Jane', age: 25}, 
  d:{name:'Luke', age: 55},
  e:{name:'Martha', age: '16'}
  ...and so on
}

How to achieve that ?

I am using JS & React

Mario Nezmah
  • 497
  • 2
  • 6
  • 19

3 Answers3

3

You can easily achieve the result using reduce and String.fromCharCode.

ASCII CODE of c is 99

const data = [
  { name: "Jane", age: 25 },
  { name: "Luke", age: 55 },
  { name: "Martha", age: "16" },
];

const result = data.reduce((acc, curr, i) => {
  acc[String.fromCharCode(99 + i)] = curr;
  return acc;
}, {});

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
2

Disregarding the why, and the limitations of using letters...

You can map the array, and create a tuples of [letter, object], and then convert them to an object using Object.fromEntries().

To create the letter, add 99 to the current index value (i) to get the current letters ascii code, and then convert it to a letter using String.charFromCode().

const data = [
  {name: 'Jane', age: 25},
  {name:'Luke', age: 55},
  {name:'Martha', age: '16'},
]

const result = Object.fromEntries(
  data.map((o, i) => [String.fromCharCode(i + 99), o])
)

console.log(result)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

You can do that... (with a simple reduce)

const data =
  [ { name: 'Jane',  age: 25  }
  , { name:'Luke',   age: 55  }
  , { name:'Martha', age: '16'}
  ]
let lCod  = 'c'.charCodeAt(0)
const res = data.reduce((r,o)=>(r[String.fromCharCode(lCod++)]=o,r),{})

console.log (res)
.as-console-wrapper { max-height: 100% !important; top: 0 }

if your array size is bigger than 24...
with the help of georg's answer

const data = 
  [ { name: 'nam_0',  age: 0 }, { name: 'nam_1',  age: 1 }, { name: 'nam_2',  age: 2 }, { name: 'nam_3',  age: 3 }, { name: 'nam_4',  age: 4 }
  , { name: 'nam_5',  age: 5 }, { name: 'nam_6',  age: 6 }, { name: 'nam_7',  age: 7 }, { name: 'nam_8',  age: 8 }, { name: 'nam_9',  age: 9 }
  , { name: 'nam_10', age:10 }, { name: 'nam_11', age:11 }, { name: 'nam_12', age:12 }, { name: 'nam_13', age:13 }, { name: 'nam_14', age:14 }
  , { name: 'nam_15', age:15 }, { name: 'nam_16', age:16 }, { name: 'nam_17', age:17 }, { name: 'nam_18', age:18 }, { name: 'nam_19', age:19 }
  , { name: 'nam_20', age:20 }, { name: 'nam_21', age:21 }, { name: 'nam_22', age:22 }, { name: 'nam_23', age:23 }, { name: 'nam_24', age:24 }
  , { name: 'nam_25', age:25 }, { name: 'nam_26', age:26 }, { name: 'nam_27', age:27 }, { name: 'nam_28', age:28 }, { name: 'nam_29', age:29 }
  ] 
, cName = ((lZero = 'c')=>
  {
  let[a,z,Ln] = [...'az'+lZero].map(c=>c.charCodeAt(0)), mod = z-a+1;
  Ln -= a;
  return ()=>
    {
    let n = Ln++, s = '';
    while (n>=0)
      { 
      s = String.fromCharCode(n % mod + a ) + s
      n = Math.floor(n / mod) - 1;
      }
    return s
  } })()
, res = data.reduce((r,o)=>(r[cName()]=o,r),{})

console.log (res)
.as-console-wrapper { max-height: 100% !important; top: 0 }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40