I need to normalize data from https://dummyjson.com API into the correct form for reduxjs consumption.
I got the data from https://dummyjson.com/posts?limit=5&skip=10
{ "posts": [ { "id": 11, "title": "It wasn't quite yet time to panic.", "body": "It wast time to salvage the situation, but he continued to delude himself into believing there was.", "tags": [ "mystery", "american", "history" ], "reactions": { "likes": 453, "dislikes": 8 }, "views": 984, "userId": 43 }, { "id": 12, "title": "She was aware that things could go wrong.", "body": "Shd no idea just how wrong everything would go that day.", "tags": [ "love", "english" ], "reactions": { "likes": 362, "dislikes": 45 }, "views": 480, "userId": 82 }, ... ] }
I’m trying to normalize them using a schema:
const post = new schema.Entity('post'); export const postsSchema = { posts: [post], };
I got the following:
{ "entities": { "post": { "1": { "id": 1, "title": "His mother had always taught him", "body": "Hidddnd.", "tags": [ "history", "american", "crime" ], "reactions": { "likes": 192, "dislikes": 25 }, "views": 305, "userId": 121 }, "2": { "id": 2, "title": "He was an expert but not in a discipline", "body": "He wddddt.", "tags": [ "french", "fiction", "english" ], "reactions": { "likes": 859, "dislikes": 32 }, "views": 4884, "userId": 91 }, } }, "result": { "posts": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "total": 251 } }
But I need the following:
{ "total": 251 "entities": { "post": { "1": { "id": 1, "title": "His mother had always taught him", "body": "Hidddnd.", "tags": [ "history", "american", "crime" ], "reactions": { "likes": 192, "dislikes": 25 }, "views": 305, "userId": 121 }, "2": { "id": 2, "title": "He was an expert but not in a discipline", "body": "He wddddt.", "tags": [ "french", "fiction", "english" ], "reactions": { "likes": 859, "dislikes": 32 }, "views": 4884, "userId": 91 }, } }, "result": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }
Thanks for help guys !
New contributor
Yaroslav Bondar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.