Im trying to filter posts by categories from this array
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)
How can i go inside post_categories that i haven't figured out. Here is the code I've done so far :
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)
}
This shows an undefined is not an object error for result.post_categories[0].id How can i access that id, inside post_categories so i can filter the results afterwards? Right now in front page it shows all the posts. After a button is clicked the toggleFilter function is triggered and it should show only the posts with that category.
From the first array after aplying the filter the desired output should go to this array :
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",
},
Here is the button that should filter the results :
<Button shadowless style={styles.buttonStyle} onPress={() => { toggleFilter(1) }} >
CodePudding user response:
You can add [0] after arr to find Ex: index[0].inIndex
I really don't understand your problem.
CodePudding user response:
Just correct me if I'm wrong. So what i understood is you have an array of objects and you want to filter the post_categories
array based on its parent category. Right?
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.map(el => ({...el, post_categories: el.post_categories.filter(post => post.id === el.category)})))
Is this what you are looking for?