Reputation: 3451
I finally gave up and wrote a for
loop to initialize a simple array of objects where each object has an incremented counter (id
) as an attribute of the object. In other words, I just want:
var sampleData = [{id: 1},{id: 2},...];
I was hoping for a compact syntax I could just put on my return statement.
let sampleData = [];
for (var p = 0; p < 25; p++){
sampleData.push({id: p});
}
return {
data: sampleData,
isLoading: true
};
Upvotes: 35
Views: 14627
Reputation: 1256
What I usually do is this:
const data = Array(10).fill().map((v, i) => ({id: i + 1}));
console.log({data});
fill
ensures it can be used with map
Upvotes: 19
Reputation: 135197
You're looking for an anamorphism, or reverse fold –
// unfold : ((r, state) -> List r, unit -> 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 } ]
You can get an intuition for how unfold
works by seeing it used in other common programs –
// unfold : ((r, state) -> List r, unit -> 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 ]
The implementation of unfold
is just one possibility. Get tinkering and implement it in a way of your choosing –
// type Maybe a = Nothing | Just a
// Just : a -> Maybe a
const Just = x =>
({ match: ({ Just: f }) => f (x) })
// Nothing : unit -> Maybe a
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 ]
I cheated a little above using a []
as a tuple. This kept the program shorter but it's better to explicitly model things and consider their types. You tagged this question with functional-programming so it's worth going the extra inch to remove this kind of implicit handling from our programs. By showing this as a separate step, we isolate a technique that can be applied not just to unfold
, but for any program we design –
// type Maybe a = Nothing | Just a
// type Tuple a b = { first: a, second: b }
// Just : a -> Maybe a
const Just = x =>
({ match: ({ Just: f }) => f (x) })
// Nothing : unit -> Maybe a
const Nothing = () =>
({ match: ({ Nothing: f }) => f () })
// Tuple : (a, b) -> Tuple a 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 ]
Upvotes: 6
Reputation: 92440
Array.from()
is a nice way to do this. You can pass a {length: somlength}
object or some other array-like object and a function that defines each item. The first argument (calling it _
just to indicate it's not used) to that function would be the item from an array we passed in (but we only passed in a length so it doesn't mean much), the second i
is the index, which is used for your id
:
let sampleData = Array.from({length: 10}, (_, id) => ({id}))
console.log(sampleData)
Upvotes: 59
Reputation: 12364
You can use a simple recursive process to do that.
const iter = (arr, counter) => {
if (counter === 25) return arr;
return iter([...arr, {id:counter}], counter + 1)
}
iter([], 0)
Upvotes: 3
Reputation: 2397
The .from()
example is great but if you really want to get creative check this out.
const newArray = length => [...`${Math.pow(10, length) - 1}`]
newArray(2)
newArray(10)
Massively limited though
newArray(1000)
["I", "n", "f", "i", "n", "i", "t", "y"]
Upvotes: 4
Reputation: 5472
You can use spread
operator with Array
and then map each undefined
element to the object that you want.
var arr = [...Array(10)].map((_,i)=>({id:i}));
console.log(arr)
Upvotes: 9