如何从数组中的对象获取帖子类别

Posted

技术标签:

【中文标题】如何从数组中的对象获取帖子类别【英文标题】:How to get post categories from an object in array 【发布时间】:2022-01-18 11:47:42 【问题描述】:

我试图从这个数组中按类别过滤帖子

Array [
  Object 
    "category": 1,
    "content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.

[Link1](https://cso-web.herokuapp.com)",
    "content_preview": null,
    "coverImageSubtitle": null,
    "coverImageTitle": null,
    "created_at": "2021-05-13T18:34:17.260Z",
    "custom_link": null,
    "gallery": Array [],
    "id": 1,
    "isDeleted": null,
    "locale": "sq",
    "localizations": Array [],
    "pages": Array [
      Object 
        "commitments": null,
        "content_sub_menu": null,
        "created_at": "2021-05-19T21:19:15.101Z",
        "id": 18,
        "isActive": true,
        "locale": "sq",
        "name": "Member Listing",
        "showOnFooterMenu": null,
        "showOnMainMenu": null,
        "slug": "member-listing",
        "template": "member-listing",
        "updated_at": "2021-09-28T09:29:56.383Z",
      ,
      Object 
        "commitments": null,
        "content_sub_menu": null,
        "created_at": "2021-05-19T21:33:28.500Z",
        "id": 22,
        "isActive": true,
        "locale": "sq",
        "name": "Evaluation Process / AL",
        "showOnFooterMenu": null,
        "showOnMainMenu": null,
        "slug": "evaluation-process",
        "template": "join-now",
        "updated_at": "2021-05-19T21:33:29.331Z",
      ,
    ],
    "post_categories": Array [
      Object 
        "created_at": "2021-05-20T15:54:36.949Z",
        "id": 1,
        "locale": "sq",
        "name": "Lajmet",
        "published_at": "2021-05-20T15:54:45.222Z",
        "updated_at": "2021-05-20T15:54:46.179Z",
      ,
    ],
    "published_at": "2021-05-13T18:34:17.260Z",
    "slider": Array [
      Object 
        "fixed": null,
        "id": 6,
        "image": Array [
          Object 
            "alternativeText": "",
            "caption": "",
            "created_at": "2021-05-13T18:34:10.259Z",
            "ext": ".svg",
            "formats": null,
            "hash": "feature_image_297dbc6cd8",
            "height": 450,
            "id": 11,
            "mime": "image/svg+xml",
            "name": "feature-image.svg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "size": 54.94,
            "updated_at": "2021-05-13T18:34:10.522Z",
            "url": "/uploads/feature_image_297dbc6cd8.svg",
            "width": 600,
          ,
        ],
        "parallax": null,
      ,
    ],
    "sort": null,
    "subTitle": null,
    "thumbnail": null,
    "title": "Our Vision, Mission and Guiding Principles",
    "updated_at": "2021-06-22T09:27:39.155Z",
  ,
  Object 
    "category": null,
    "content": "It is a quick and easy way for self-evaluating an organization in relation to the Global Standard for CSO Accountability, aiming at identifying strengths and areas for improvement.

<img src=\"/uploads/logo_2_319de9613c.jpeg\" style=\"width: 100px; height: 150px\" />

Legacy giving requires thought, lots of time and technical skills. So how do you make it as easy as possible for your donors to consider pledging a gift in a will? What decision science nudges can you use to help them along the decision-making process?

In this session you will learn the fundamentals of decision science, how people make decisions and which nudges you can use in your gifts in wills marketing to transform your appeals and secure pledges.

Learning Outcomes

1) Understand how designing legacy marketing differs from other fundraising materials
2) Learn how to leverage decision science nudges to inspire donors to consider leaving a gift in a will
3) How to write impactful legacy marketing",
    "content_preview": null,
    "coverImageSubtitle": null,
    "coverImageTitle": null,
    "created_at": "2021-05-25T21:21:03.979Z",
    "custom_link": null,
    "gallery": Array [],
    "id": 8,
    "isDeleted": null,
    "locale": "sq",
    "localizations": Array [],
    "pages": Array [],
    "post_categories": Array [],
    "published_at": "2021-05-25T21:22:12.934Z",
    "slider": Array [],
    "sort": null,
    "subTitle": "Përparimi i karrierës suaj në një botë post pandemike",
    "thumbnail": null,
    "title": "Publikim CSO",
    "updated_at": "2021-05-25T21:22:13.300Z",
  ,
  Object 
    "category": 2,
    "content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.
For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.


- [CSO Report 2020-2021](/)
- [CSO Report 2020-2021](/)",
    "content_preview": null,
    "coverImageSubtitle": "adfasdf asdf asdf asdfsdf asdf ahsfahs dfhsdhha hhhahsd fhasd fhas dfhsahdf ahsdf ashdf hasdhf has dfh asdfhashdf. h sda fashdf hasd fhashd f ashdf hashdf ",
    "coverImageTitle": "Learn everything About CSO",
    "created_at": "2021-05-15T08:55:42.292Z",
    "custom_link": null,
    "gallery": Array [],
    "id": 2,
    "isDeleted": null,
    "locale": "sq",
    "localizations": Array [],
    "pages": Array [],
    "post_categories": Array [],
    "published_at": "2021-05-15T08:55:42.292Z",
    "slider": Array [
      Object 
        "fixed": true,
        "id": 7,
        "image": Array [
          Object 
            "alternativeText": "",
            "caption": "",
            "created_at": "2021-05-13T18:34:10.259Z",
            "ext": ".svg",
            "formats": null,
            "hash": "feature_image_297dbc6cd8",
            "height": 450,
            "id": 11,
            "mime": "image/svg+xml",
            "name": "feature-image.svg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "size": 54.94,
            "updated_at": "2021-05-13T18:34:10.522Z",
            "url": "/uploads/feature_image_297dbc6cd8.svg",
            "width": 600,
          ,
        ],
        "parallax": null,
      ,
    ],
    "sort": null,
    "subTitle": "subtitle goes here",
    "thumbnail": null,
    "title": "Our Vision, Mission and Guiding Principles",
    "updated_at": "2021-06-22T09:27:13.855Z",
  ,
  Object 
    "category": null,
    "content": "",
    "content_preview": "A jeni gati për t`u bërë anëtar i Kodit? <br/>
Testoni Standardet aktuale të organizatës tuaj [këtu](/evaluation).",
    "coverImageSubtitle": null,
    "coverImageTitle": null,
    "created_at": "2021-07-30T15:37:09.909Z",
    "custom_link": "/join-now",
    "gallery": Array [],
    "id": 58,
    "isDeleted": null,
    "locale": "sq",
    "localizations": Array [
      Object 
        "id": 61,
        "locale": "en",
        "published_at": "2021-09-06T08:12:34.668Z",
      ,
    ],
    "pages": Array [
      Object 
        "commitments": null,
        "content_sub_menu": 2,
        "created_at": "2021-05-19T13:17:54.278Z",
        "id": 10,
        "isActive": true,
        "locale": "sq",
        "name": "Rreth Kodit",
        "showOnFooterMenu": null,
        "showOnMainMenu": true,
        "slug": "about",
        "template": "about",
        "updated_at": "2021-07-28T09:43:03.711Z",
      ,
    ],
    "post_categories": Array [],
    "published_at": "2021-07-30T15:37:25.717Z",
    "slider": Array [
      Object 
        "fixed": false,
        "id": 99,
        "image": Array [
          Object 
            "alternativeText": "",
            "caption": "",
            "created_at": "2021-06-22T11:21:15.147Z",
            "ext": ".1&auto=format&fit=crop&w=1350&q=80",
            "formats": Object 
              "large": Object 
                "ext": ".1&auto=format&fit=crop&w=1350&q=80",
                "hash": "large_photo_1524178232363_1fb2b075b655_ixid_Mnwx_Mj_A3f_DB_8_M_Hxwa_G90by1w_Y_Wdlf_Hx8f_G_Vuf_DB_8f_Hx8_and_ixlib_rb_1_2_df5889d144",
                "height": 667,
                "mime": "image/jpeg",
                "name": "large_https://images.unsplash.com/photo-1524178232363-1fb2b075b655?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80",
                "path": null,
                "size": 93.66,
                "url": "/uploads/large_photo_1524178232363_1fb2b075b655_ixid_Mnwx_Mj_A3f_DB_8_M_Hxwa_G90by1w_Y_Wdlf_Hx8f_G_Vuf_DB_8f_Hx8_and_ixlib_rb_1_2_df5889d144.1&auto=format&fit=crop&w=1350&q=80",
                "width": 1000,
              ,
              "medium": Object 
                "ext": ".1&auto=format&fit=crop&w=1350&q=80",
                "hash": "medium_photo_1524178232363_1fb2b075b655_ixid_Mnwx_Mj_A3f_DB_8_M_Hxwa_G90by1w_Y_Wdlf_Hx8f_G_Vuf_DB_8f_Hx8_and_ixlib_rb_1_2_df5889d144",
                "height": 500,
                "mime": "image/jpeg",
                "name": "medium_https://images.unsplash.com/photo-1524178232363-1fb2b075b655?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80",
                "path": null,
                "size": 59.18,
                "url": "/uploads/medium_photo_1524178232...(truncated to the first 10000 characters)

我怎样才能进入我还没有弄清楚的 post_categories。 这是我到目前为止所做的代码:

const [results, setResults] = useState([]);
  const [errorMessage, setErrorMessage] = useState('');
  const [category, setCategory] = useState(null);
  const [filteredData, setFilteredData] = useState([]);

  const filteredResults = useMemo(() => results.filter(result => category === null || result.post_categories[0].id === category), [results, category]);
  const toggleFilter = cat => 
    setCategory(c => cat === c ? null : cat);
    setResults(results)
  

这表明 result.post_categories[0].id 的 undefined is not an object 错误 如何在 post_categories 中访问该 ID,以便之后过滤结果? 现在在首页它显示了所有的帖子。点击按钮后,toggleFilter 功能被触发,它应该只显示属于该类别的帖子。

从应用过滤器后的第一个数组开始,所需的输出应该转到这个数组:

Array [
  Object 
    "category": 1,
    "content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.

[Link1](https://cso-web.herokuapp.com)",
    "content_preview": null,
    "coverImageSubtitle": null,
    "coverImageTitle": null,
    "created_at": "2021-05-13T18:34:17.260Z",
    "custom_link": null,
    "gallery": Array [],
    "id": 1,
    "isDeleted": null,
    "locale": "sq",
    "localizations": Array [],
    "pages": Array [
      Object 
        "commitments": null,
        "content_sub_menu": null,
        "created_at": "2021-05-19T21:19:15.101Z",
        "id": 18,
        "isActive": true,
        "locale": "sq",
        "name": "Member Listing",
        "showOnFooterMenu": null,
        "showOnMainMenu": null,
        "slug": "member-listing",
        "template": "member-listing",
        "updated_at": "2021-09-28T09:29:56.383Z",
      ,
      Object 
        "commitments": null,
        "content_sub_menu": null,
        "created_at": "2021-05-19T21:33:28.500Z",
        "id": 22,
        "isActive": true,
        "locale": "sq",
        "name": "Evaluation Process / AL",
        "showOnFooterMenu": null,
        "showOnMainMenu": null,
        "slug": "evaluation-process",
        "template": "join-now",
        "updated_at": "2021-05-19T21:33:29.331Z",
      ,
    ],
    "post_categories": Array [
      Object 
        "created_at": "2021-05-20T15:54:36.949Z",
        "id": 1,
        "locale": "sq",
        "name": "Lajmet",
        "published_at": "2021-05-20T15:54:45.222Z",
        "updated_at": "2021-05-20T15:54:46.179Z",
      ,
    ],
    "published_at": "2021-05-13T18:34:17.260Z",
    "slider": Array [
      Object 
        "fixed": null,
        "id": 6,
        "image": Array [
          Object 
            "alternativeText": "",
            "caption": "",
            "created_at": "2021-05-13T18:34:10.259Z",
            "ext": ".svg",
            "formats": null,
            "hash": "feature_image_297dbc6cd8",
            "height": 450,
            "id": 11,
            "mime": "image/svg+xml",
            "name": "feature-image.svg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "size": 54.94,
            "updated_at": "2021-05-13T18:34:10.522Z",
            "url": "/uploads/feature_image_297dbc6cd8.svg",
            "width": 600,
          ,
        ],
        "parallax": null,
      ,
    ],
    "sort": null,
    "subTitle": null,
    "thumbnail": null,
    "title": "Our Vision, Mission and Guiding Principles",
    "updated_at": "2021-06-22T09:27:39.155Z",
  ,
 Object 
    "category": 2,
    "content": "test",
    "content_preview": null,
    "coverImageSubtitle": null,
    "coverImageTitle": null,
    "created_at": "2021-05-13T18:34:17.260Z",
    "custom_link": null,
    "gallery": Array [],
    "id": 1,
    "isDeleted": null,
    "locale": "sq",
    "localizations": Array [],
    "pages": Array [
      Object 
        "commitments": null,
        "content_sub_menu": null,
        "created_at": "2021-05-19T21:19:15.101Z",
        "id": 18,
        "isActive": true,
        "locale": "sq",
        "name": "Member Listing",
        "showOnFooterMenu": null,
        "showOnMainMenu": null,
        "slug": "member-listing",
        "template": "member-listing",
        "updated_at": "2021-09-28T09:29:56.383Z",
      ,
      Object 
        "commitments": null,
        "content_sub_menu": null,
        "created_at": "2021-05-19T21:33:28.500Z",
        "id": 22,
        "isActive": true,
        "locale": "sq",
        "name": "Evaluation Process / AL",
        "showOnFooterMenu": null,
        "showOnMainMenu": null,
        "slug": "evaluation-process",
        "template": "join-now",
        "updated_at": "2021-05-19T21:33:29.331Z",
      ,
    ],
    "post_categories": Array [
      Object 
        "created_at": "2021-05-20T15:54:36.949Z",
        "id": 1,
        "locale": "sq",
        "name": "Lajmet",
        "published_at": "2021-05-20T15:54:45.222Z",
        "updated_at": "2021-05-20T15:54:46.179Z",
      ,
    ],
    "published_at": "2021-05-13T18:34:17.260Z",
    "slider": Array [
      Object 
        "fixed": null,
        "id": 6,
        "image": Array [
          Object 
            "alternativeText": "",
            "caption": "",
            "created_at": "2021-05-13T18:34:10.259Z",
            "ext": ".svg",
            "formats": null,
            "hash": "feature_image_297dbc6cd8",
            "height": 450,
            "id": 11,
            "mime": "image/svg+xml",
            "name": "feature-image.svg",
            "previewUrl": null,
            "provider": "local",
            "provider_metadata": null,
            "size": 54.94,
            "updated_at": "2021-05-13T18:34:10.522Z",
            "url": "/uploads/feature_image_297dbc6cd8.svg",
            "width": 600,
          ,
        ],
        "parallax": null,
      ,
    ],
    "sort": null,
    "subTitle": null,
    "thumbnail": null,
    "title": "Our Vision, Mission and Guiding Principles",
    "updated_at": "2021-06-22T09:27:39.155Z",
  ,

这是应该过滤结果的按钮:

<Button shadowless style=styles.buttonStyle onPress=() =>  toggleFilter(1)  >

【问题讨论】:

您可以添加实际的数组和所需输出的示例吗? 这是来自 api 端点的实际数组。我将添加所需的输出,检查编辑后的版本 @NeelDsouza 你现在可以检查一下吗 我的意思是期望的输出,你想要的过滤数组。以便我们更了解您的问题。我们不会通过 UI 图像来理解。 我添加了它@NeelDsouza,但输出应该是仅返回具有该类别 ID 的那些帖子的数组。我够清楚了吗,我的英语不太好,抱歉 【参考方案1】:

您会收到未定义的错误,因为在少数情况下,post_categories 数组是空的,如果您尝试访问第 0 个元素,它将引发错误。因此,为数组长度和 id 添加一个空检查,如下所示

const arr = [
    
      "category": 1,
      "content": "For 50 years, AFP has been the standard-bearer for professionalism in fundraising. Learn more about AFP, its activities and people, and how you can be involved.",
      "content_preview": null,
      "coverImageSubtitle": null,
      "coverImageTitle": null,
      "created_at": "2021-05-13T18:34:17.260Z",
      "custom_link": null,
      "gallery": [],
      "id": 1,
      "isDeleted": null,
      "locale": "sq",
      "localizations": [],
      "pages": [
        
          "commitments": null,
          "content_sub_menu": null,
          "created_at": "2021-05-19T21:19:15.101Z",
          "id": 18,
          "isActive": true,
          "locale": "sq",
          "name": "Member Listing",
          "showOnFooterMenu": null,
          "showOnMainMenu": null,
          "slug": "member-listing",
          "template": "member-listing",
          "updated_at": "2021-09-28T09:29:56.383Z",
        ,
        
          "commitments": null,
          "content_sub_menu": null,
          "created_at": "2021-05-19T21:33:28.500Z",
          "id": 22,
          "isActive": true,
          "locale": "sq",
          "name": "Evaluation Process / AL",
          "showOnFooterMenu": null,
          "showOnMainMenu": null,
          "slug": "evaluation-process",
          "template": "join-now",
          "updated_at": "2021-05-19T21:33:29.331Z",
        ,
      ],
      "post_categories": [
        
          "created_at": "2021-05-20T15:54:36.949Z",
          "id": 1,
          "locale": "sq",
          "name": "Lajmet",
          "published_at": "2021-05-20T15:54:45.222Z",
          "updated_at": "2021-05-20T15:54:46.179Z",
        ,
        
          "created_at": "2021-05-20T15:54:36.949Z",
          "id": 2,
          "locale": "sq",
          "name": "Lajmet",
          "published_at": "2021-05-20T15:54:45.222Z",
          "updated_at": "2021-05-20T15:54:46.179Z",
        ,
      ],
      "published_at": "2021-05-13T18:34:17.260Z",
      "slider": [
        
          "fixed": null,
          "id": 6,
          "image": [
            
              "alternativeText": "",
              "caption": "",
              "created_at": "2021-05-13T18:34:10.259Z",
              "ext": ".svg",
              "formats": null,
              "hash": "feature_image_297dbc6cd8",
              "height": 450,
              "id": 11,
              "mime": "image/svg+xml",
              "name": "feature-image.svg",
              "previewUrl": null,
              "provider": "local",
              "provider_metadata": null,
              "size": 54.94,
              "updated_at": "2021-05-13T18:34:10.522Z",
              "url": "/uploads/feature_image_297dbc6cd8.svg",
              "width": 600,
            ,
          ],
          "parallax": null,
        ,
      ],
      "sort": null,
      "subTitle": null,
      "thumbnail": null,
      "title": "Our Vision, Mission and Guiding Principles",
      "updated_at": "2021-06-22T09:27:39.155Z",
    ]

  console.log(arr.filter(el => el.post_categories.length > 0 && el.id === el.post_categories[0]?.id))
  // console.log(arr.map(el => (...el, post_categories: el.post_categories.filter(post => post.id === el.category))))
  

这是你要找的吗?

【讨论】:

感谢您的回答,很抱歉不清楚。我想要做的是根据 post_categories 过滤帖子数组,忽略数组第一行中不需要的类别。数组结构有点像这样 `arr [ post1 : id: 1 content: 'test' post_categories id : 1 , post2 : id: 2 content: 'test' post_categories: id : 2 ] `现在我只想要 post1 一个,例如,我如何过滤它 还是有点混乱。 post_categories 又是一个数组。我们如何在帖子中检查?您的意思是 post_categories[0].id 应该出现在帖子中并对其进行过滤吗? 是的,post_categories 是数组中的一个数组,例如 post1 或 post2。这就是api端点的结构。是的,我试图像 post_categories[0].id 这样在该数组中导航,但是当我这样做时它显示一个未定义的错误 更新了答案 感谢您的帮助,现在可以使用了 :)

以上是关于如何从数组中的对象获取帖子类别的主要内容,如果未能解决你的问题,请参考以下文章

如何从模板中的自定义帖子类型中仅提取一个类别?

从 Wordpress 中自定义帖子类型的类别中获取 ACF 文本字段值

在 wordpress 中获取 2 级类别中的下一个、上一个帖子

根据 phpmyadmin 中的类别 ID 获取 wordpress 帖子 ID 列表

如何从存储在 mongodb 中的对象中获取日期?

从帖子 ID 获取类别名称