使用React处理包含对象数组的上下文

huangapple go评论63阅读模式
英文:

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: &#39;hats&#39;,
items: [
{
id: 1,
name: &#39;Brown Brim&#39;,
imageUrl: &#39;https://i.ibb.co/ZYW3VTp/brown-brim.png&#39;,
price: 25,
},
{
id: 2,
name: &#39;Blue Beanie&#39;,
imageUrl: &#39;https://i.ibb.co/ypkgK0X/blue-beanie.png&#39;,
price: 18,
},
{
id: 3,
name: &#39;Brown Cowboy&#39;,
imageUrl: &#39;https://i.ibb.co/QdJwgmp/brown-cowboy.png&#39;,
price: 35,
},
{
id: 4,
name: &#39;Grey Brim&#39;,
imageUrl: &#39;https://i.ibb.co/RjBLWxB/grey-brim.png&#39;,
price: 25,
},
{
id: 5,
name: &#39;Green Beanie&#39;,
imageUrl: &#39;https://i.ibb.co/YTjW3vF/green-beanie.png&#39;,
price: 18,
},
{
id: 6,
name: &#39;Palm Tree Cap&#39;,
imageUrl: &#39;https://i.ibb.co/rKBDvJX/palm-tree-cap.png&#39;,
price: 14,
},
{
id: 7,
name: &#39;Red Beanie&#39;,
imageUrl: &#39;https://i.ibb.co/bLB646Z/red-beanie.png&#39;,
price: 18,
},
{
id: 8,
name: &#39;Wolf Cap&#39;,
imageUrl: &#39;https://i.ibb.co/1f2nWMM/wolf-cap.png&#39;,
price: 14,
},
{
id: 9,
name: &#39;Blue Snapback&#39;,
imageUrl: &#39;https://i.ibb.co/X2VJP2W/blue-snapback.png&#39;,
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 = &#39;hat&#39;;
// Filter the categories to find the one that contains the word &#39;hat&#39;
const categoryWithHat = categoriesMap.find(category =&gt; category.title.includes(searchTerm));
if (categoryWithHat) {
const title = categoryWithHat.title;
console.log(title); // Output: &#39;hats&#39;
// Access the items within the category
const items = categoryWithHat.items;
console.log(items); // Output: Array of items
} else {
console.log(&#39;No category found with the word &quot;hat&quot;&#39;);
}

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 = ({}) =&gt; {
// log.console(&quot;cat&quot;, {category});
let { category } = useParams();
//console.log(&quot;ca: &quot;,category);  
//category = &quot;hats&quot;;
const { categoriesMap } = useContext(CategoriesContext);
const [products, setProducts] = useState(categoriesMap[category]);
console.log(&quot;category :&quot;, categoriesMap.title);
const searchTerm = &quot;hats&quot;;
const categoriesMapWithHat = categoriesMap.find(category =&gt; category.title.includes(searchTerm));
const title = categoriesMapWithHat.title;
alert(&quot;title: &quot;, 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 = ({}) =&gt; {
// log.console(&quot;cat&quot;, {category});
let { category } = useParams();
//console.log(&quot;ca: &quot;,category);  
//category = &quot;hats&quot;;
const { categoriesMap } = useContext(CategoriesContext);
const [products, setProducts] = useState(categoriesMap[category]);
console.log(&quot;category :&quot;, categoriesMap.title);
const searchTerm = &quot;hats&quot;;
const categoriesMapWithHat = categoriesMap.find(category =&gt; category.title.includes(searchTerm));
const title = categoriesMapWithHat.title;
alert(&quot;title: &quot;, title);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 08:23:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定