---
# System prepended metadata

title: '2. Array to Object '

---

<!-- JSJGFV7 -->
<!-- arrays -->

### Q. Can you convert an Array to object in javascript?

_**Intent of the interview:** To assess the candidate's knowledge of manipulating data structures in JavaScript._

**Answer:**
Yes, there are several ways to convert an array into an object in JavaScript. You can use the `reduce()` array method. This method iterates over the array and builds an object with keys and values.

```js
const arrayToConvert = ["red", "green", "blue"];
const convertedObject = arrayToConvert.reduce((obj, item, index) => {
  obj[index] = item;
  return obj;
}, {});

console.log(convertedObject);
```

result:

```js
{
  0: "red"
  1: "green"
  2: "blue"
}
```

In this object, the array indexes become the object keys, and the array values become the object values.

<details>
<summary>More Info</summary>
<br/>

Here are a few more methods:

1. **Using the `Object.assign()` method:**

This is another direct way to convert an array into an object, where indices become keys.

```js
let arr = ["red", "green", "blue"];
let obj = Object.assign({}, arr);
```

result:

```js
{
  0: "red"
  1: "green"
  2: "blue"
}
```

2. **Using a `for` loop:**

Iterate over the array and set properties manually.


```js    
let arr = ["red", "green", "blue"];
let obj = {};

for (let i = 0; i < arr.length; i++) {
  obj[i] = arr[i];
}
```

result:

```js
{
  0: "red"
  1: "green"
  2: "blue"
}
```

3. **Using `Array.prototype.forEach` method:**

This is a more declarative approach compared to the traditional `for` loop.

```js
let arr = ["red", "green", "blue"];
let obj = {};

arr.forEach((value, index) => {
  obj[index] = value;
});
```

result:

```js
{
  0: "red"
  1: "green"
  2: "blue"
}
```

4. **Using `Map` with `Object.fromEntries`:**

Convert the array into a map of key-value pairs and then use `Object.fromEntries` to generate an object.

```js
let arr = ["red", "green", "blue"];
let map = new Map(arr.map((value, index) => [index.toString(), value]));
let obj = Object.fromEntries(map);
```

result:

```js
{
  0: "red"
  1: "green"
  2: "blue"
}
```

Depending on the specific requirements, one can choose the method that fits best. Some methods are more readable, while others are more concise.

</details>



----
    

#### NXT MOCK CONTENT


----
    
#### QUESTIONS
    
@QUESTION_KEY: 1
@QUESTION_ID: 8a7b9c1d-57aa-4d9d-8d8a-5f9b9c4a9a64
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

What is the output of the following code?

```js
const arrayToConvert = ["red", "green", "blue"];

const convertedObject = arrayToConvert.reduce((obj, item, index) => {
  obj[index] = item;
  return obj;
}, {});

console.log(convertedObject);
```

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1:

```js
{
  0: "red"
  1: "green"
  2: "blue"
}
```

@OPTION2:

```js
{
  "red": "0",
  "green": "1",
  "blue": "2"
}
```

@OPTION3:

```js
{
  'red': 'red',
  'green': 'green',
  'blue': 'blue'
}
```

@OPTION4:

```js
{
  "0": "0",
  "1": "1",
  "2": "2"
}
```

@CORRECT_OPTIONS: OPTION1
@TAG_NAMES: arrays

---END

@QUESTION_KEY: 2
@QUESTION_ID: 9b7c9d1e-58bb-4d9e-8d9b-6f9c9e4a9a75
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

What will the value of `convertedObject[1]` be after the code is executed?

```js
const arrayToConvert = ["red", "green", "blue"];
const convertedObject = arrayToConvert.reduce((obj, item, index) => {
  obj[index] = item;
  return obj;
}, {});
```

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1: "red"
@OPTION2: "green"
@OPTION3: "blue"
@OPTION4: Undefined
@CORRECT_OPTIONS: OPTION2
@TAG_NAMES: arrays

---END

@QUESTION_KEY: 3
@QUESTION_ID: 9b7c9d1e-58bb-4d9e-8d9b-6f9c9e4a9a75
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

What does the reduce method do in the provided code?

```js
const arrayToConvert = ["red", "green", "blue"];
const convertedObject = arrayToConvert.reduce((obj, item, index) => {
  obj[index] = item;
  return obj;
}, {});
```

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1: It filters the array.
@OPTION2: It sorts the array.
@OPTION3: It converts the array to a string.
@OPTION4: It converts the array to an object.
@CORRECT_OPTIONS: OPTION4
@TAG_NAMES: arrays

---END

@QUESTION_KEY: 4
@QUESTION_ID: 10c8d1f-59cc-4d9f-8d9c-7f9d9f4a9a86
@QUESTION_TYPE: MULTIPLE_CHOICE

@QUESTION:

Which of the following codes will convert the array ["apple", "banana", "cherry"] to an object where the keys are the array values and the values are their lengths?

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1:

```js
["apple", "banana", "cherry"].reduce((obj, item) => {
  obj[item] = item.length;
  return obj;
}, {});
```

@OPTION2:

```js
["apple", "banana", "cherry"].map((item) => {
  return { item: item.length };
});
```

@OPTION3:

```js
["apple", "banana", "cherry"].forEach((item) => {
  return { item: item.length };
});
```

@OPTION4:

```js
["apple", "banana", "cherry"].filter((item) => {
  return { item: item.length };
});
```

@CORRECT_OPTIONS: OPTION1
@TAG_NAMES: arrays

---END

@QUESTION_KEY: 5
@QUESTION_ID: 7e292647-9e47-41d5-b70b-5b828d4a08fd
@QUESTION_TYPE: MORE_THAN_ONE_MULTIPLE_CHOICE

@QUESTION:

Which code will give the required result if you want to convert the array `["cat", "dog"]` into an object with keys as the string values and values as the string lengths?

@QUESTION_CONTENT_TYPE: MARKDOWN

@OPTION1:

```js
["cat", "dog"].reduce((obj, item) => {
  obj[item] = item.length;
  return obj;
}, {});
```

@OPTION2:

```js
["cat", "dog"].map((item) => {
  return { [item]: item.length };
});
```

@OPTION3:

```js
["cat", "dog"].forEach((item) => {
  return { [item]: item.length };
});
```

@OPTION4:

```js
["cat", "dog"].filter((item) => {
  return { [item]: item.length };
});
```

@CORRECT_OPTIONS: OPTION1
@TAG_NAMES: arrays

---END
