JavaScript Interview Question I faced recently 💻 Convert this array "["a","1","b","2","c","3"]" into this object "{ a: 1, b: 2, c: 3 }" const arr = ["a","1","b","2","c","3"]; const obj = {}; for (let i = 0; i < arr.length; i += 2) { obj[arr[i]] = arr[i + 1]; } console.log(obj); // { a: "1", b: "2", c: "3" } Simple logic: The array has key–value pairs, so we loop through it two elements at a time. #JavaScript #FrontendDeveloper #CodingInterview
we can this same with reduce method const arr = ["a","1","b","2","c","3"]; const obj = arr.reduce((acc, curr, index) => { if (index % 2 === 0) { acc[curr] = Number(arr[index + 1]); } return acc; }, {}); console.log(obj);
Nice question. It would be great if you could also mention the experience level for which this was asked in the interview. It helps others understand the expected difficulty.
Yes ,good can you post some more questions,on js problems
Keep sharing
same question asked for my friend for a intern role in 5minutes timeframe