Using Map Object to Render React Components in Order - reactjs

I receive an array of product objects like such from my backend.
products = [
{id: 1, price: 10, category: "food", name: "apples"},
{id: 2, price: 5, category: "supplies", name: "spoons"},
{id: 3, price: 15, category: "pets", name: "cat treats"},
{id: 4, price: 9, category: "food", name: "oranges"},
// ...
]
I am looking to render all my products as strips based on their category in a specific order of priority (similar to how Netflix aligns their shows). I don't want them displayed in random order, such as the category strip of food being at the top, the next category strip being pets, etc.
I currently decided to create a map object since it keeps order as followed:
let filteredProducts = new Map([
["food", []],
["pets", []],
["supplies", []],
// ...
])
for (let product of products) {
let category = product["category"]
filteredProducts.get(category).push(product)
}
The goal is to then iterate through the map object and return multiple React Component of category strips from top to bottom.
My question is, I can't help but feel that there are more efficient ways to render this as I don't see map objects being utilized commonly. Are there more efficient and frequently used ways to filter and render by priority or is this in the right direction?

I do believe there is more easier way to do this using Array sort function.
Please consider the snippet below.
const products = [
{id: 1, price: 10, category: "food", name: "apples"},
{id: 2, price: 5, category: "supplies", name: "spoons"},
{id: 3, price: 15, category: "pets", name: "cat treats"},
{id: 4, price: 9, category: "food", name: "oranges"},
// ...
]
const priority = [
'food',
'supplies',
'pets'
]
const orderedProducts = products.sort(
(p1, p2) => priority.indexOf(p1.category) - priority.indexOf(p2.category)
);
Let me know if this looks much cleaner & easier approach.

Related

Nested duplicate mongoose document on generation tree

I'm creating a project which is store a tree of generation of a family, i'm strugle while update a list when reordered the list.
I'm currently updated each person's childrens but i have duplicate person object between the root list and the child list.
For example, i have a list with 3 persons:
[
{id: 1, name: 'Duc Tran', children: []},
{id: 2, name: 'Jackie Tran', children: []}
{id: 3, name: 'Duck Tran', children: []}
]
and after move the person with id: 1 into the person with id: 2 then i wan't it to show the array like this
[
{id: 2, name: 'Jackie Tran', children: [
{id: 1, name: 'Duc Tran', children: []}
]}
{id: 3, name: 'Duck Tran', children: []}
]
Is there a way to do is at mongoose, or does it have any way to update a whole list of document ?

How sort when have array with name and score mongodb

I have a document with users and each user haves "categories" and "tags", in both as at next:
[
{user_id: 1, categories:[
{category: uuidv4x1, score:75},
{category: uuidv4x2, score:67}
],
tags: [
{tag: "1ne", score:15},
{tag: "subjetive", score:89}
]}
How can order by category, tag but too by score, if i have:
[
{id: 1, category: uuidv4x6, tags:["1ne", "optimized"]},
{id: 2, category: uuidv4x2, tags:["1ne", "subjetive"]},
{id: 3, category: uuidv4x1, tags:["home", "car"]}
]
Before to all, thanks so much.
I expect sort my find for better matches, customize articles.

Check, if in .map (), that object exists in another array React

How can I see in the map of products if that product is in the favorites?
Log of products
[
{id: 1, product_name: “product one”},
{id: 2, product_name: “product two”},
{id: 3, product_name: “product three”}
]
Log of favorites
[
{id: 1, product_name: “product one”},
{id: 2, product_name: “product two”}
]
{products.map((item} =>
// Here, if the product is in the favorites list then show x else show y
// I need to check here if the product is in the favorites list, to show two different types of icons
)
I don’t know how to do, I also tried to map to favorites with product id = favorite product id, but it didn't work
var products = [{
id: 1,
product_name: 'product one'
},
{
id: 2,
product_name: 'product two'
},
{
id: 3,
product_name: 'product three'
}
]
var favorites = [{
id: 1,
product_name: 'product one'
},
{
id: 2,
product_name: 'product two'
}
]
const solution = products.map((product)=>{
return favorites.find(el=>el.product_name === product.product_name) ? 'x' : 'y'
})

How to Manage the response of the API in react?

i did a request to the API, then the response is as the following:
data1 = [{id: 1, code:1, title:title1}, {id:2, code:3, title:title2}, ...]
Now i would like to extract an array of the titles of the above response as below:
titles = [title1, title2, ....]
how can i do it by map?
And the second question:
Consider if the response would be as follow:
data2 = [{id: 1, code:1, title:title1, chapters:[{id:1, chapter: chapter1},{id:2, chapter: chapter2}, ...]}, {id:4, code:5, title:title3, chapters:[{id:4, chapter: chapter3}, ...]}, ...]
In this case how can i extract an array of the chapters as following:
chapters = [chapter1, chapter2, chapter3]
I tried to do them as below:
for the first question:
title = data1.map((item) => {item.title})
for the second one i did:
chapters = data2.chapters.map((item) => {item.chapter})
But it doesn't work. I think some where there are error in syntaxes.
Can any one help me how to manage these data?
Thank you.
Yep, you are wrong with syntax.
Firs case - title = data1.map((item) => {item.title})
You've wrapped item.title with {}, so you should add return. Or omit {}.
For example: title = data1.map(item => item.title)
Second case - same issue with {}, but you should also use flatMap because you need flat list in result. If you write with regular map - you won't get desired ["chapter1", "chapter2"].
See also detailed example below.
const data1 = [
{ id: 1, code: 1, title: "title1" },
{ id: 2, code: 3, title: "title2" }
];
const data1_mapped = data1.map(d => d.title);
console.log(data1_mapped);
const data2 = [
{
id: 1,
code: 1,
title: "title1",
chapters: [{ id: 1, chapter: "chapter1" }, { id: 2, chapter: "chapter2" }]
},
{
id: 2,
code: 2,
title: "title2",
chapters: [{ id: 1, chapter: "chapter22" }, { id: 2, chapter: "chapter32" }]
}
];
const data2_mapped = data2.flatMap(d => d.chapters.map(c => c.chapter));
console.log(data2_mapped);
You are not returning a value. Try removing braces like so...
title = data1.map((item) => item.title)
chapters = data2.chapters.map((item) => item.chapter)
See this for more info on the issue:
Meaning of curly braces in array.map()

Finding values from a nested object

I have an array like so:
[
{
lookups: [],
rows: [{data: {a: 1, b: 2}}, {data: {a: 3, b: 4}}],
title: "Table 1",
columns: [{name: "a"}, {name: "b"}]
},
{
lookups: [],
rows: [{data: {c: 5, d: 6}}, {data: {c: 7, d: 8}}],
title: "Table 2",
columns: [{name: "c"}, {name: "d"}]
}
]
After I select the correct object (which I am using Array.find() using the title to do). I need to be able to go through the rows array and try to get each value for a particular string that I have (e.g. If I have a string value of "a" then I would like to get the values 1 and 3 back).
Help would be very much appreciated.
Thanks for your time.
Use simple mapping. Suppose you have property name stored in variable named prop
data = obj.rows.map(({data}) => data[prop])

Resources