-
Notifications
You must be signed in to change notification settings - Fork 1
/
57-array-transformation-TASK.js
63 lines (58 loc) · 1.15 KB
/
57-array-transformation-TASK.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/** TASK 57 - Transforming an Array of Objects
*
* 1. Create a "processPosts" function that will return a new array of posts
*
* 2. Please note that
* - some property names in each post changed
* - ID of each message increased by 1000
*
* 3. The original array of posts must remain unchanged
*/
/* the new array should look like this
[
{
postId: 1234,
postAuthor: 'robd',
postCommentsQty: 5
},
{
postId: 1823,
postAuthor: 'sady',
postCommentsQty: 0
},
{
postId: 1161,
postAuthor: 'merryl',
postCommentsQty: 8
}
]
*/
const processPosts = (inputPostsArray) => {
return inputPostsArray.map((post) => {
return {
postId: post.postId + 1000,
postAuthor: post.author,
postCommentsQty: post.commentsQty === undefined ? 0 : post.commentsQty,
};
});
};
const testPosts = [
{
postId: 234,
author: 'robd',
commentsQty: 5,
},
{
postId: 823,
author: 'sady',
},
{
postId: 161,
author: 'merryl',
commentsQty: 8,
},
];
const processedPosts = processPosts(testPosts);
console.log(processedPosts);
console.log(testPosts);
// original array must remain unchanged