-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdenormalizing_data.js
More file actions
48 lines (43 loc) · 1.4 KB
/
denormalizing_data.js
File metadata and controls
48 lines (43 loc) · 1.4 KB
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
// denormalizing data (client side "join")
const users = [
{ userId: 101, name: "Rahim" },
{ userId: 102, name: "Karim" },
{ userId: 103, name: "Amina" },
{ userId: 104, name: "Nusrat" },
{ userId: 105, name: "Sakib" },
{ userId: 106, name: "Tanvir" },
{ userId: 107, name: "Hasan" },
{ userId: 108, name: "Mim" },
{ userId: 109, name: "Jahid" },
{ userId: 110, name: "Arif" },
];
const posts = [
{ id: 1, userId: 101, title: "Learn JavaScript Basics" },
{ id: 2, userId: 102, title: "Understanding ES6 Features" },
{ id: 3, userId: 103, title: "Intro to React" },
{ id: 4, userId: 101, title: "Advanced React Hooks" },
{ id: 5, userId: 104, title: "CSS Flexbox Guide" },
{ id: 6, userId: 102, title: "Mastering CSS Grid" },
{ id: 7, userId: 105, title: "Node.js for Beginners" },
{ id: 8, userId: 103, title: "Express.js Crash Course" },
{ id: 9, userId: 101, title: "MongoDB Basics" },
{ id: 10, userId: 104, title: "Full Stack Development Guide" },
];
// create hash table of posts
const postByUserId = posts.reduce((table, post) => {
const { userId } = post;
if (!table[userId]) {
table[userId] = [];
}
table[userId].push(post);
return table;
}, {});
console.log(postByUserId);
const userWithPost = users.map((user) => {
return {
...user,
post: postByUserId[user.userId] || [],
};
});
// console.log(postByUserId);
console.log(JSON.stringify(userWithPost));