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"}
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"}
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)
let Arr = ["a","b","'c','d'","e"].reduce((acc, val, index) => {
acc[`temp${index}`] = val;
return acc;
}, {});
console.log(Arr);
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);
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);