[英]Create array of objects based on length; each object has a property with the index + 1 as its value
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 } ]);
}
Create an array with Array
, and use Array.prototype.map
.使用Array
创建一个数组,并使用Array.prototype.map
。
function func(foo) { return Array(foo).fill().map((f, i) => ({ year: i + 1 })); } console.log(func(1)); console.log(func(3));
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).这假定此语句位于 function 内(否则, return
将不起作用)。
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 .更合理的长度可能是 Math.max(0, Math.min(2 ** 32 - 1 Math.max(0, Math.min(2 ** 32 - 1, Math.floor(this.foo)))
,它将长度限制为至少 0 和最多 2 32 - 1, 最大数组长度。
If you want to always have at least one element in your array, then use Math.max(1,
… )
instead.如果您希望数组中始终至少有一个元素,请改用Math.max(1,
... )
。
Returning an assignment (ie return this.result =
… ;
) is frowned upon .返回一个作业(即return this.result =
... ;
)是不受欢迎的。 The best practice is to separate these statements 2 :最佳做法是将这些语句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. Array.from
与 arguments 一起使用:第一个是“类似数组”的 object { length: Math.max(0, Math.min(2 ** 32 - 1, Math.floor(this.foo))) }
它被转换为正确的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).在这种情况下,将初始化一个长度为Math.floor(this.foo)
的数组(限制在可能的数组长度范围内)。 The second argument maps each value at a certain index
to an object with the year
property with the value index + 1
.第二个参数将某个index
处的每个值映射到 object ,其中year
属性的值index + 1
。 _value
isn't used (it's undefined
anyway). _value
未使用(无论如何它是undefined
的)。
The advantage to use Array.from(
array-like ,
mapping-function )
over eg .fill().map(
… )
is that only a single array is created in memory.使用Array.from(
array-like ,
mapping-function )
优于.fill().map(
… )
的优点是在 memory 中只创建了一个数组。 Array(
… ).fill().map(
… )
creates two new arrays: the first one at Array(
… ).fill()
, the second one at .map(
… )
. Array(
... ).fill().map(
... )
创建两个新的 arrays:第一个在Array(
... ).fill()
,第二个在.map(
... )
。 Therefore, the Array.from
approach is more memory-efficient.因此, Array.from
方法更节省内存。
1 : Maybe soon we'll finally get a Math.clamp
method… 1 :也许很快我们就会得到一个Math.clamp
方法……
2 : Technically, this would be equivalent. 2 :从技术上讲,这将是等效的。 If this.result
is a getter / setter, there may be an observable difference.如果this.result
是一个 getter / setter,可能会有明显的差异。
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;
try this oneliner试试这个oneliner
let x=3;
let result = [ ...Array(x).keys() ].map(item=> {return{"year":item}});
console.log(result);
let arr = [];
for(let i = 1; i<this.foo;i++){
arr.push({ 'year':i})
};
console.log(arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.