I want to create an object with every value as "true" and the length of keys will be same with array length.
Example :
const myCar = [{name:ford,type:A},{name:opel,type:B}]
The output want to be like this below
{0:true,1:true}
If the length from the array myCar will be 3 so the output will be like this as well:
{0:true,1:true,2:true}
>Solution :
You can reduce to get a counter and an object
const myCar = [{ name: 'ford', type: 'A' }, { name: 'opel', type: 'B' }];
const myObj = myCar.reduce((acc,_,i) => (acc[i] = true,acc),{});
console.log(myObj);
Breakdown
const myObj = myCar
.reduce(
(acc,_,i) => // reduce passes the accumulator, the current object which we ignore here and the index
(acc[i] = true // set the accumulator at key=index to true
,acc) // and use the comma operator to return the accumulator
,{}); // initialise the accumulator to an empty object