@@ -10,30 +10,39 @@ npm i populate-array
1010yarn add populate-array
1111```
1212
13+ ## Import
14+ ``` javascript
15+ // ES6:
16+ import {populateArray } from ' populate-array' ;
17+ // CommonJS:
18+ var {populateArray} = require (' populate-array' );
19+ ```
20+
1321## Usage
1422
1523Let's say we have an array of users where each user country is an iso2 code.
1624We want this to be the country object instead when sending it to the client side.
1725
18- ``` javascript
26+ ``` typescript
27+ import {populateArray } from " populate-array" ;
28+
1929const users = [
20- {
21- id: 1 ,
22- name: ' John Doe' ,
23- country: ' US'
24- },
25- // ... and so on
30+ {
31+ id: 1 ,
32+ name: ' John Doe' ,
33+ country: ' US'
34+ },
35+ // ... and so on
2636];
2737
2838// Normal way
2939for (const user of users ) {
30- user .country = getCountryFn (user .country )
40+ user .country = getCountryFn (user .country )
3141}
3242
3343// Populate way
34- populateArray (users, {
35- path: ' country' ,
36- each: getCountryFn
44+ populateArray (users , ' country' , {
45+ each: getCountryFn
3746});
3847
3948// Result:
@@ -74,8 +83,7 @@ for (const user of users) {
7483}
7584
7685// Populate way
77- populateArray (users, {
78- path: ' country' ,
86+ populateArray (users, ' country' , {
7987 as: ' countryData' ,
8088 each: getCountryFn
8189});
@@ -123,7 +131,7 @@ populateArray(users, {
123131### unique
124132
125133If true, the ` each ` function will be called only once for a unique ` path ` value object.
126- Note: Your ` path ` value must be string-able for this to work.
134+ Note: Your ` path ` value must be a ** string** or ** number ** for this to work.
127135
128136``` javascript
129137const users = [
@@ -134,8 +142,7 @@ const users = [
134142 // ... and so on
135143];
136144
137- populateArray (users, {
138- path: ' country' ,
145+ populateArray (users, ' country' , {
139146 unique: true ,
140147 each : (pathValue ) => {
141148 // The each function will be called only 3 times
@@ -147,5 +154,39 @@ populateArray(users, {
147154
148155### use
149156
150- A function that provides any data used to populate
157+ A function that provides any data that can be used to populate.
158+
159+ Using a database example: A case scenario where we want to populate the ` userId ` property of a ` posts ` array.
160+
161+ ``` javascript
162+ // The `posts` array is an array of objects
163+ // where each object has a `userId` property
164+ // and we want to populate the `user` property
165+ // with the user object from the database
166+
167+ const posts = [
168+ {id: 1 , userId: 1 , title: ' Post 1' },
169+ {id: 2 , userId: 2 , title: ' Post 2' },
170+ {id: 3 , userId: 2 , title: ' Post 3' },
171+ {id: 4 , userId: 1 , title: ' Post 4' },
172+ ]
173+
174+ populateArray (posts, ' userId' , {
175+ use (userIds ) {
176+ // `userIds` is an array of the `userId` values
177+ // i.e [1, 2, 2, 1]
178+ // Assuming we are using a mongodb like database
179+ return Users .find ({_id: {$in: userIds}});
180+ },
181+ each (userId , users ) {
182+ // `userId` is the value of the `userId` property
183+ // `users` is the value returned by the `use` function
184+ // now we loop through the users array and return the user object
185+ // that matches the `userId` value
186+ // This is faster/better than calling the database on each iteration.
187+ // in terms of performance.
188+ return users .find (user => user .id === userId);
189+ }
190+ });
191+ ```
151192
0 commit comments