英文:
Using Context with an Object of Arrays using React
问题
我正在研究在React应用程序中使用上下文。我不确定如何描述接下来的内容,但我在考虑它可能是一个对象数组。我的问题是如何提取包含单词'hat'的标题。这只是一个项目。否则,它可能包含更多这些项目,我也想提取它们。我尝试过的是:categoriesMap[0]等等。没有起作用。谢谢,Joshua。
categoriesMap[category] =
[
{
title: 'hats',
items: [
{
id: 1,
name: 'Brown Brim',
imageUrl: 'https://i.ibb.co/ZYW3VTp/brown-brim.png',
price: 25,
},
{
id: 2,
name: 'Blue Beanie',
imageUrl: 'https://i.ibb.co/ypkgK0X/blue-beanie.png',
price: 18,
},
{
id: 3,
name: 'Brown Cowboy',
imageUrl: 'https://i.ibb.co/QdJwgmp/brown-cowboy.png',
price: 35,
},
{
id: 4,
name: 'Grey Brim',
imageUrl: 'https://i.ibb.co/RjBLWxB/grey-brim.png',
price: 25,
},
{
id: 5,
name: 'Green Beanie',
imageUrl: 'https://i.ibb.co/YTjW3vF/green-beanie.png',
price: 18,
},
{
id: 6,
name: 'Palm Tree Cap',
imageUrl: 'https://i.ibb.co/rKBDvJX/palm-tree-cap.png',
price: 14,
},
{
id: 7,
name: 'Red Beanie',
imageUrl: 'https://i.ibb.co/bLB646Z/red-beanie.png',
price: 18,
},
{
id: 8,
name: 'Wolf Cap',
imageUrl: 'https://i.ibb.co/1f2nWMM/wolf-cap.png',
price: 14,
},
{
id: 9,
name: 'Blue Snapback',
imageUrl: 'https://i.ibb.co/X2VJP2W/blue-snapback.png',
price: 16,
},
],
},
];
英文:
I am working on using context in react applications. I am not sure how to describe what follows, but I am thinking maybe it is an object of arrays. My question is how I extract the title which contains the word, 'hat'. This is just one item. Otherwise, it would contain more of these items that I would also extract. What I tried is: categoriesMap[0] and the like. Nothing worked. Thanks, Joshua.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
categoriesMap[category] =
[
{
title: 'hats',
items: [
{
id: 1,
name: 'Brown Brim',
imageUrl: 'https://i.ibb.co/ZYW3VTp/brown-brim.png',
price: 25,
},
{
id: 2,
name: 'Blue Beanie',
imageUrl: 'https://i.ibb.co/ypkgK0X/blue-beanie.png',
price: 18,
},
{
id: 3,
name: 'Brown Cowboy',
imageUrl: 'https://i.ibb.co/QdJwgmp/brown-cowboy.png',
price: 35,
},
{
id: 4,
name: 'Grey Brim',
imageUrl: 'https://i.ibb.co/RjBLWxB/grey-brim.png',
price: 25,
},
{
id: 5,
name: 'Green Beanie',
imageUrl: 'https://i.ibb.co/YTjW3vF/green-beanie.png',
price: 18,
},
{
id: 6,
name: 'Palm Tree Cap',
imageUrl: 'https://i.ibb.co/rKBDvJX/palm-tree-cap.png',
price: 14,
},
{
id: 7,
name: 'Red Beanie',
imageUrl: 'https://i.ibb.co/bLB646Z/red-beanie.png',
price: 18,
},
{
id: 8,
name: 'Wolf Cap',
imageUrl: 'https://i.ibb.co/1f2nWMM/wolf-cap.png',
price: 14,
},
{
id: 9,
name: 'Blue Snapback',
imageUrl: 'https://i.ibb.co/X2VJP2W/blue-snapback.png',
price: 16,
},
],
},
];
<!-- end snippet -->
答案1
得分: 2
从categoriesMap中提取包含单词'hat'的标题,您可以使用filter和find数组方法。以下是您可以实现此目标的示例:
const searchTerm = 'hat';
// 过滤categories以找到包含单词'hat'的类别
const categoryWithHat = categoriesMap.find(category => category.title.includes(searchTerm));
if (categoryWithHat) {
const title = categoryWithHat.title;
console.log(title); // 输出:'hats'
// 访问类别内的项目
const items = categoryWithHat.items;
console.log(items); // 输出:项目数组
} else {
console.log('未找到包含单词"hat"的类别');
}
在这段代码中,使用find方法来过滤categoriesMap数组,并找到标题包含单词'hat'的类别。如果找到匹配的类别,可以访问其标题和项目。否则,如果未找到匹配的类别,可以显示相应的消息。
请记得将categoriesMap替换为您在代码中实际使用的变量名称。
英文:
To extract the title that contains the word 'hat' from the categoriesMap, you can use the filter and find array methods. Here's an example of how you can achieve that:
const searchTerm = 'hat';
// Filter the categories to find the one that contains the word 'hat'
const categoryWithHat = categoriesMap.find(category => category.title.includes(searchTerm));
if (categoryWithHat) {
const title = categoryWithHat.title;
console.log(title); // Output: 'hats'
// Access the items within the category
const items = categoryWithHat.items;
console.log(items); // Output: Array of items
} else {
console.log('No category found with the word "hat"');
}
In this code, the find method is used to filter the categoriesMap array and find the category that has the title containing the word 'hat'. If a matching category is found, its title and items can be accessed. Otherwise, if no matching category is found, an appropriate message can be displayed.
Remember to replace categoriesMap with the actual variable name you're using in your code.
答案2
得分: 0
I'm here to help with the translation. Here's the translated code portion:
啊,我无法让查找功能正常工作,我得到了以下错误信息:categoriesMap.find 不是一个函数。也许是因为它不是一个简单的数组? 请继续帮助我,好吗? 这是代码:
<!-- 开始代码段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-js -->
const Category = ({}) => {
// log.console("cat", {category});
let { category } = useParams();
//console.log("ca: ",category);
//category = "hats";
const { categoriesMap } = useContext(CategoriesContext);
const [products, setProducts] = useState(categoriesMap[category]);
console.log("category :", categoriesMap.title);
const searchTerm = "hats";
const categoriesMapWithHat = categoriesMap.find(category => category.title.includes(searchTerm));
const title = categoriesMapWithHat.title;
alert("title: ", title);
<!-- 结束代码段 -->
Please note that I've retained the code structure and only translated the comments and error message.
英文:
Ah, I can't get the find to work, I get: categoriesMap.find is not a function.
Perhaps because it is not a simple array? Would you continue to help me, please? Here is the code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const Category = ({}) => {
// log.console("cat", {category});
let { category } = useParams();
//console.log("ca: ",category);
//category = "hats";
const { categoriesMap } = useContext(CategoriesContext);
const [products, setProducts] = useState(categoriesMap[category]);
console.log("category :", categoriesMap.title);
const searchTerm = "hats";
const categoriesMapWithHat = categoriesMap.find(category => category.title.includes(searchTerm));
const title = categoriesMapWithHat.title;
alert("title: ", title);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论