# Create array of objects based on length; each object has a property with the index + 1 as its value

###### Posted By: Anonymous

I want to improve my ugly code to something more clean and simple.

How can I shorten this code?

```
if (this.foo < 2) {
return (this.result = [ { year: 1 } ]);
}
if (this.foo < 3) {
return (this.result = [ { year: 1 }, { year: 2 } ]);
}
if (this.foo < 4) {
return (this.result = [ { year: 1 }, { year: 2 }, { year: 3 } ]);
}
if (this.foo < 5) {
return (this.result = [ { year: 1 }, { year: 2 }, { year: 3 }, { year: 4 } ]);
}
if (this.foo < 6) {
return (this.result = [ { year: 1 }, { year: 2 }, { year: 3 }, { year: 4 }, { year: 5 } ]);
}
```

## Solution

What youâ€™ve shown is equivalent to this (except this goes beyond five elements, but also beyond one in the other direction):

```
return this.result = Array.from({
length: Math.floor(this.foo)
}, (_value, index) => ({
year: index + 1
}));
```

This assumes that this statement is inside a function (otherwise, `return`

wouldnâ€™t work).

A more reasonable length may be `Math.max(0, Math.min(2 ** 32 - 1, Math.floor(this.foo)))`

which clamps^{1} the length to at least 0 and at most 2^{32} âˆ’ 1, the maximum Array length.

If you want to always have at least one element in your array, then use `Math.max(1,`

â€¦`)`

instead.

Returning an assignment (i.e. `return this.result =`

â€¦`;`

) is frowned upon.

The best practice is to separate these statements^{2}:

```
this.result = Array.from({
length: Math.max(0, Math.min(2 ** 32 - 1, Math.floor(this.foo)))
}, (_value, index) => ({
year: index + 1
}));
return this.result;
```

`Array.from`

is used with both arguments: the first one is the â€œarray-likeâ€ object `{ length: Math.max(0, Math.min(2 ** 32 - 1, Math.floor(this.foo))) }`

which gets converted to a proper `Array`

object.

In this case, an array with a length of `Math.floor(this.foo)`

is initialized (clamped to the range of possible Array lengths).

The second argument maps each value at a certain `index`

to an object with the `year`

property with the value `index + 1`

.

`_value`

isnâ€™t used (itâ€™s `undefined`

anyway).

The advantage to use `Array.from(`

*array-like*`,`

*mapping-function*`)`

over e.g. `.fill().map(`

â€¦`)`

is that only a single array is created in memory.

`Array(`

â€¦`).fill().map(`

â€¦`)`

creates *two* new arrays: the first one at `Array(`

â€¦`).fill()`

, the second one at `.map(`

â€¦`)`

.

Therefore, the `Array.from`

approach is more memory-efficient.

^{1}: Maybe soon weâ€™ll finally get a `Math.clamp`

methodâ€¦

^{2}: Technically, this would be equivalent.

If `this.result`

is a getter / setter, there may be an observable difference.

```
const result = Array.from({
length: Math.max(0, Math.min(2 ** 32 - 1, Math.floor(this.foo)))
}, (_value, index) => ({
year: index + 1
}));
this.result = result;
return result;
```

###### Answered By: Anonymous

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.