我终于放弃并写了一个for
循环来初始化一个简单的对象数组,其中每个对象都有一个递增的计数器(id
)作为对象的属性。换句话说,我只想要:
var sampleData = [{id: 1},{id: 2},...];
我希望有一个紧凑的语法,我可以把它放在我的返回语句上。
let sampleData = [];
for (var p = 0; p < 25; p++){
sampleData.push({id: p});
}
return {
data: sampleData,
isLoading: true
};
Array.from()
是一个很好的方式来做到这一点。您可以传递{length: somlength}
对象或其他类似数组的对象以及定义每个项目的函数。第一个参数(称为_
只是为了表明它没有被使用)到该函数将是我们传入的数组中的项(但我们只传入一个长度所以它并不意味着太多),第二个i
是索引,用于你的id
:
let sampleData = Array.from({length: 10}, (_, id) => ({id}))
console.log(sampleData)
我通常做的是:
const data = Array(10).fill().map((v, i) => ({id: i + 1}))
fill
确保它可以与map
一起使用
您可以将spread
运算符与Array
一起使用,然后将每个undefined
元素映射到您想要的对象。
var arr = [...Array(10)].map((_,i)=>({id:i}));
console.log(arr)
你正在寻找一个变形,或反向折叠 -
// unfold : ( (r, state) -> List r, () -> List r, state ) -> List r
const unfold = (f, init) =>
f ( (x, next) => [ x, ...unfold (f, next) ]
, () => []
, init
)
// sampleData : List { id: Int }
const sampleData =
unfold
( (next, done, i) =>
i > 25
? done ()
: next ({ id: i }, i + 1)
, 0
)
console.log (sampleData)
// [ { id: 0 }, { id : 1 }, ... { id: 25 } ]
你可以通过看到它在其他常见程序中使用来获得unfold
如何工作的直觉 -
// unfold : ( (r, state) -> List r, () -> List r, state ) -> List r
const unfold = (f, init) =>
f ( (x, next) => [ x, ...unfold (f, next) ]
, () => []
, init
)
// fibseq : Int -> List Int
const fibseq = init =>
unfold
( (next, done, [ n, a, b ]) =>
n === 0
? done ()
: next (a, [ n - 1, b, a + b ])
, [ init, 0, 1 ]
)
console.log (fibseq (10))
// [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]
unfold
的实施只是一种可能性。以您选择的方式进行修补和实施 -
// type Maybe a = Nothing | Just a
const Just = x =>
({ match: ({ Just: f }) => f (x) })
const Nothing = () =>
({ match: ({ Nothing: f }) => f () })
// unfold : (state -> Maybe (a, state), state) -> List a
const unfold = (f, init) =>
f (init) .match
( { Nothing: () => []
, Just: ([ x, next ]) => [ x, ...unfold (f, next) ]
}
)
// fibseq : Int -> List Int
const fibseq = init =>
unfold
( ([ n, a, b ]) =>
n === 0
? Nothing ()
: Just ([ a, [ n - 1, b, a + b ] ]) // <-- yikes, read more below
, [ init, 0, 1 ]
)
console.log (fibseq (10))
// [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]
我使用[]
作为元组欺骗了一点。这使程序更短,但最好明确地模拟事物并考虑它们的类型。你用功能编程标记了这个问题,所以值得花费额外的英寸来从我们的程序中删除这种隐式处理。通过将此作为一个单独的步骤,我们分离出一种技术,不仅可以应用于unfold
,而且可以应用于我们设计的任何程序 -
// type Maybe a = Nothing | Just a
const Just = x =>
({ match: ({ Just: f }) => f (x) })
const Nothing = () =>
({ match: ({ Nothing: f }) => f () })
// type Tuple a b = { first: a, second: b }
const Tuple = (first, second) =>
({ first, second })
// unfold : (state -> Maybe Tuple (a, state), state) -> List a
const unfold = (f, init) =>
f (init) .match
( { Nothing: () => []
, Just: (t) => [ t.first, ...unfold (f, t.second) ] // <-- Tuple
}
)
// fibseq : Int -> List Int
const fibseq = init =>
unfold
( ([ n, a, b ]) =>
n === 0
? Nothing ()
: Just (Tuple (a, [ n - 1, b, a + b ])) // <-- Tuple
, [ init, 0, 1 ]
)
console.log (fibseq (10))
// [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]
.from()
的例子很棒但是如果你真的想要有创意,请查看这个。
const newArray = length => [...`${Math.pow(10, length) - 1}`]
newArray(2)
newArray(10)
但是受到严重限制
newArray(1000)
["I", "n", "f", "i", "n", "i", "t", "y"]
您可以使用简单的递归过程来执行此操作。
const iter = (arr, counter) => {
if (counter === 25) return arr;
return iter([...arr, {id:counter}], counter + 1)
}
iter([], 0)