-2
let Arr = ["a","b","'c','d'","e","f"]

need to store value in object like with fixed key name

obj = {hey: "a", hello: "b", how: "'c','d'",are: "e",you:"f"}

4 Answers4

2

You can use reduce function and inside the accumulator object create a new key with by cancating temp and index

let arr = ["a", "b", "'c','d'", "e"];

let newObj = arr.reduce((acc, curr, index) => {
  acc['temp' + index] = curr;
  return acc;
}, {});

console.log(newObj)
brk
  • 48,835
  • 10
  • 56
  • 78
1

let Arr = ["a","b","'c','d'","e"].reduce((acc, val, index) => {
  acc[`temp${index}`] = val;
  return acc;
}, {});

console.log(Arr);
Shubham
  • 1,755
  • 3
  • 17
  • 33
1

You can use Array.prototype.forEach:

let Arr = ["a", "b", "'c','d'", "e"];

let Obj = {};

Arr.forEach((s, i) => { Obj['temp' + (i || '')] = s; });

console.log(Obj);
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
0

Try

Arr.reduce((a,c,i)=> (a[keys[i]]=c,a),{});

let Arr = ["a","b","'c','d'","e","f"];
let keys = ["hey","hello","how","are","you"];

let obj = Arr.reduce((a,c,i)=> (a[keys[i]]=c,a),{});

console.log(obj);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345