How to get specific field from nested table relation in adonisjs - javascript

THIS IS MY CODE QUERY
i want to get only some specific field from every table:
const project = await Project.query()
.where("user_id", user_id)
.where('id', project_id)
.with('items.file.sheets.markup.comment.attachment').first()
but i get this response
there is lot of extra fields
"status": true,
"message": "Here is detail of Your Project",
"data": {
"id": 1,
"name": "Spacey",
"description": "222 please change some points",
"created_at": "2021-03-24 17:18:56",
"updated_at": "2021-03-24 19:12:09",
"deleted_at": null,
"items": [
{
"id": 1,
"project_id": 1,
"assignee_id": 1,
"status": null,
"type": "markup",
"description": "my ",
"version": 4,
"visibility": "1",
"due_date": "2019-12-31",
"priority": "high",
"resolved_on": null,
"created_by": 2,
"resolved_by": null,
"updated_by": null,
"created_at": "2021-03-24 17:19:02",
"updated_at": "2021-03-24 17:19:02",
"deleted_at": null,
"file": [
{
"id": 1,
"actual_name": "10Feb2021.pdf",
"original_ext": "pdf",
"random_name": "1616588342707_8063",
"original_local_path": "files/2/1/1/1616588342707_8063.pdf",
"image_url": "https://resolve-dev-backend.s3.us-east- "
"media_type": "profile",
"item_id": 1,
"user_id": 2,
"created_at": "2021-03-24 17:19:05",
"updated_at": "2021-03-24 17:19:05",
"created_by": 2,
"updated_by": null,
"deleted_at": null,
"sheets": [
{
"id": 1,
"actual_name": "convertpdftojpg.png",
"original_ext": "png",
"random_name": "1616588346705_117",
"original_local_path": "sheets/2/1/1/1616588346705_117.png",
"image_url": "https://resolve-dev-backend.s3.us-east-"
"type": "Sheet",
"item_id": 1,
"file_id": 1,
"user_id": 2,
"created_at": "2021-03-24 17:19:09",
"updated_at": "2021-03-24 17:19:09",
"created_by": 2,
"updated_by": null,
"deleted_at": null,
"markup": [
{
"id": 1,
"item_id": 1,
"sheet_id": 1,
"assignee_id": null,
"editor_details": "{\"a\": \"b\", \"c\": \"d\"}",
"visibility": "private",
"image_url": "{\"a\": \"b\", \"c\": \"d\"}",
"priority": "low",
"resolved_by": 2,
"resolved_on": "2021-03-",
"due_date": null,
"created_by": 2,
"created_at": "2021-03-24 18:30:21",
"updated_at": "2021-03-24 19:12:39",
"deleted_at": null,
"comment": []
}
]
}
]
}
],
but i don't need project_id etc like some field in every table
for
example i need only these field from file table
and want to get specific field from every table in my query
but i get above output response in my postman
```
"id": 1,
"actual_name": "10 -Rehan Shakeel - Feb2021.pdf",
"image_url": "https://resolve-dev/file/2/1/1/1616588346705_117.pdf",
"media_type": "profile",
"created_at": "2021-03-24 17:19:05",
"updated_at": "2021-03-24 17:19:05",
"deleted_at": null,
```

Try to add the select statement with the columns inside an array
const project = await Project.query()
.select(['tableyouwant.id', 'actual_name', 'image_url', 'media_type', ..........])
.where('id', project_id)
.with('items.file.sheets.markup.comment.attachment').first()

Related

It's possible to find a link in a object if I know how it starts?

I'm getting a response from an API, I'm interested only in one of the link that returns. I have been using for a while
["data"]["tasks"]["0"]["result"]["files"]["0"]["url"]
to get the URL but it seems the API has changed and now the "result" it changes the position 0 in every call...
{"data": {
"id": "dcd99049-63b2",
"tag": "jobbuilder",
"status": "finished",
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:28+00:00",
"ended_at": "2022-11-22T21:57:33+00:00",
"tasks": [
{
"id": "48c34a03-54f2",
"name": "task-2",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 0,
"code": null,
"message": null,
"percent": 100,
"operation": "convert",
"engine": "qpdf",
"engine_version": "10.1.0",
"result": {
"files": [
{
"filename": "something.pdf",
"size": 633487
}
]
},
"created_at": "2022-11-22T21:57:30+00:00",
"started_at": "2022-11-22T21:57:32+00:00",
"ended_at": "2022-11-22T21:57:32+00:00",
"retry_of_task_id": null,
"copy_of_task_id": "dd64385a-a2e9",
"user_id": 6070,
"priority": 10,
"host_name": "meggie",
"storage": null,
"depends_on_task_ids": [
"dd64385a-a2e9",
"acfd2a2c-3c45"
],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/48c34a03-54f2"
}
},
{
"id": "6333e3b8-2f6f",
"name": "export-1",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 0,
"code": null,
"message": null,
"percent": 100,
"operation": "export/url",
"result": {
"files": [
{
"filename": "something.pdf",
"size": 633487,
"url": "https://storage.cloudconvert.com/tasks/somethinglink.pdf"
}
]
},
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:32+00:00",
"ended_at": "2022-11-22T21:57:32+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 607,
"priority": 10,
"host_name": "meggie",
"storage": "ceph-fra",
"depends_on_task_ids": [
"48c34a03-54f2"
],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/6333e3b8-2f6f"
}
},
{
"id": "dd64385a-a2e9",
"name": "task-2-create-watermark",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 1,
"code": null,
"message": null,
"percent": 100,
"operation": "watermark",
"engine": "imagemagick",
"engine_version": "7.1.0",
"result": {
"files": [
{
"filename": "watermark.pdf",
"size": 609229
}
]
},
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:30+00:00",
"ended_at": "2022-11-22T21:57:32+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 6070,
"priority": 10,
"host_name": "meggie",
"storage": "ceph-fra",
"depends_on_task_ids": [
"acfd2a2c-3c45-4e47"
],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/dd64385a-a2e9"
}
},
{
"id": "acfd2a2c-3c45-4e47",
"name": "task-1",
"job_id": "dcd99049-63b2",
"status": "finished",
"credits": 1,
"code": null,
"message": null,
"percent": 100,
"operation": "capture-website",
"engine": "wkhtml",
"engine_version": "0.12.5",
"result": {
"files": [
{
"filename": "something.pdf",
"size": 27330
}
]
},
"created_at": "2022-11-22T21:57:28+00:00",
"started_at": "2022-11-22T21:57:28+00:00",
"ended_at": "2022-11-22T21:57:30+00:00",
"retry_of_task_id": null,
"copy_of_task_id": null,
"user_id": 6070,
"priority": 10,
"host_name": "meggie",
"storage": null,
"depends_on_task_ids": [],
"links": {
"self": "https://api.cloudconvert.com/v2/tasks/acfd2a2c-3c45"
}
}
],
"links": {
"self": "https://api.cloudconvert.com/v2/jobs/dcd99049-63b2-443e"
}
}};
I would like to always extract only the link:
https://storage.cloudconvert.com/tasks/somethinglink.pdf
which position can change as mentioned before... The link always start with https://storage.cloudconvert.com
I have been using this for a while to access the first object in the array
["data"]["tasks"]["0"]["result"]["files"]["0"]["url"]
but it doesn't seem to work if the link it's in a different position...
This code extracts all values as strings, then finds the one with the necessary prefix:
const data = {"data":{"id":"dcd99049-63b2","tag":"jobbuilder","status":"finished","created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:28+00:00","ended_at":"2022-11-22T21:57:33+00:00","tasks":[{"id":"48c34a03-54f2","name":"task-2","job_id":"dcd99049-63b2","status":"finished","credits":0,"code":null,"message":null,"percent":100,"operation":"convert","engine":"qpdf","engine_version":"10.1.0","result":{"files":[{"filename":"something.pdf","size":633487}]},"created_at":"2022-11-22T21:57:30+00:00","started_at":"2022-11-22T21:57:32+00:00","ended_at":"2022-11-22T21:57:32+00:00","retry_of_task_id":null,"copy_of_task_id":"dd64385a-a2e9","user_id":6070,"priority":10,"host_name":"meggie","storage":null,"depends_on_task_ids":["dd64385a-a2e9","acfd2a2c-3c45"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/48c34a03-54f2"}},{"id":"6333e3b8-2f6f","name":"export-1","job_id":"dcd99049-63b2","status":"finished","credits":0,"code":null,"message":null,"percent":100,"operation":"export/url","result":{"files":[{"filename":"something.pdf","size":633487,"url":"https://storage.cloudconvert.com/tasks/somethinglink.pdf"}]},"created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:32+00:00","ended_at":"2022-11-22T21:57:32+00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":607,"priority":10,"host_name":"meggie","storage":"ceph-fra","depends_on_task_ids":["48c34a03-54f2"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/6333e3b8-2f6f"}},{"id":"dd64385a-a2e9","name":"task-2-create-watermark","job_id":"dcd99049-63b2","status":"finished","credits":1,"code":null,"message":null,"percent":100,"operation":"watermark","engine":"imagemagick","engine_version":"7.1.0","result":{"files":[{"filename":"watermark.pdf","size":609229}]},"created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:30+00:00","ended_at":"2022-11-22T21:57:32+00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":6070,"priority":10,"host_name":"meggie","storage":"ceph-fra","depends_on_task_ids":["acfd2a2c-3c45-4e47"],"links":{"self":"https://api.cloudconvert.com/v2/tasks/dd64385a-a2e9"}},{"id":"acfd2a2c-3c45-4e47","name":"task-1","job_id":"dcd99049-63b2","status":"finished","credits":1,"code":null,"message":null,"percent":100,"operation":"capture-website","engine":"wkhtml","engine_version":"0.12.5","result":{"files":[{"filename":"something.pdf","size":27330}]},"created_at":"2022-11-22T21:57:28+00:00","started_at":"2022-11-22T21:57:28+00:00","ended_at":"2022-11-22T21:57:30+00:00","retry_of_task_id":null,"copy_of_task_id":null,"user_id":6070,"priority":10,"host_name":"meggie","storage":null,"depends_on_task_ids":[],"links":{"self":"https://api.cloudconvert.com/v2/tasks/acfd2a2c-3c45"}}],"links":{"self":"https://api.cloudconvert.com/v2/jobs/dcd99049-63b2-443e"}}}
const f=i=>i instanceof Object?Object.values(i).flatMap(v=>f(v)):[i+'']
console.log(f(data).find(i=>
i.startsWith('https://storage.cloudconvert.com/tasks/')))

How to get post categories from an object in array

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) }} >
You are getting the undefined error because for few of the cases the post_categories array is empty and if u try accessing the 0th element it will throw an error. So add a null check for the array length and for id something like below
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)})))
Is this what you are looking for?

Javascript: how overwrite/rename the keys of an object?

Let's say I have this object
[{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}]
I would get another object:
[{
"value": 2,
"text": "admin#example.com",
}, {
"value": 3,
"text": "agency_owner#example.com",
}, {
"value": 4,
"text": "license_owner#example.com",
}]
So, renaming "id" with "value" and "email" with "text".
I would use map and maybe not installing lodash or similar...
Thank you...
Well, map is a great idea. What is stopping you from using it?
You can:
obj = [{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}]
obj = obj.map(e => { return {
value: e.id,
text: e.email
}})
console.log(obj)
You can use map as you mentioned:
var arr = [{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}];
var result = arr.map(o => {
return { value: o.id, email: o.email}
});
console.log(result);
[{
"id": 2,
"email": "admin#example.com",
"role_id": 1,
"is_active": 1,
"created_at": "2020-10-10T17:05:34.000000Z",
"updated_at": "2020-10-10T17:05:34.000000Z",
"deleted_at": null
}, {
"id": 3,
"email": "agency_owner#example.com",
"role_id": 2,
"is_active": 1,
"created_at": "2020-10-11T18:30:06.000000Z",
"updated_at": "2020-10-11T18:33:51.000000Z",
"deleted_at": null
}].map(function(obj){
return {
value:obj.id,
text:obj.email
}
})

For to mount json

I have in variable bookUnitIdInformacoes this array of objects:
[
{
"id": 5,
"book_id": 33,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:21",
"updated_at": "2019-12-30 14:54:12",
"miniature": null
},
{
"id": 6,
"book_id": 33,
"unit": 1,
"sequence": 2,
"description": "UNIT_01_VOCABULARY",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:39",
"updated_at": "2019-12-27 08:11:39",
"miniature": null
},
{
"id": 7,
"book_id": 33,
"unit": 2,
"sequence": 1,
"description": "UNIT_02_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:46",
"updated_at": "2019-12-27 08:11:46",
"miniature": null
},
{
"id": 8,
"book_id": 39,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:07:09",
"updated_at": "2019-12-30 15:03:50",
"miniature": null
}
]
I have in the variable idioma this array of objects:
[
{
"id": 13,
"code": "ING-NOT-2019",
"description": "Inglês Noturno 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Noturno",
"language": "Inglês",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 10:04:47",
"updated_at": "2020-01-05 16:08:00",
"language_substring": "US"
},
{
"id": 14,
"code": "ESP-MAN-2019",
"description": "Espanhol manhã 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Manhã",
"language": "Espanhol",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:06:44",
"updated_at": "2019-12-30 11:06:44",
"language_substring": null
}
]
I need to create a for() that while the column book_id is equal the index+1, insert in idioma[i].quiz the value of the bookUnitIdInformacoes[i] and when the book_id of the bookUnitIdInformacoes array is different, put in the next position of idioma[i]quiz, so i need this json:
[
{
"id": 13,
"code": "ING-NOT-2019",
"description": "Inglês Noturno 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Noturno",
"language": "Inglês",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 10:04:47",
"updated_at": "2020-01-05 16:08:00",
"language_substring": "US",
"quiz": [
{
"id": 5,
"book_id": 33,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:21",
"updated_at": "2019-12-30 14:54:12",
"miniature": null
},
{
"id": 6,
"book_id": 33,
"unit": 1,
"sequence": 2,
"description": "UNIT_01_VOCABULARY",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:39",
"updated_at": "2019-12-27 08:11:39",
"miniature": null
},
{
"id": 7,
"book_id": 33,
"unit": 2,
"sequence": 1,
"description": "UNIT_02_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-27 08:11:46",
"updated_at": "2019-12-27 08:11:46",
"miniature": null
}
]
},
{
"id": 14,
"code": "ESP-MAN-2019",
"description": "Espanhol manhã 2019",
"start_date": "2019-12-30T03:00:00.000Z",
"end_date": "2019-12-31T03:00:00.000Z",
"period": "Manhã",
"language": "Espanhol",
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:06:44",
"updated_at": "2019-12-30 11:06:44",
"language_substring": null,
"quiz": [
{
"id": 8,
"book_id": 39,
"unit": 1,
"sequence": 1,
"description": "UNIT_01_GRAMMAR",
"qt_question": 5,
"status": false,
"user_id": 1,
"created_at": "2019-12-30 11:07:09",
"updated_at": "2019-12-30 15:03:50",
"miniature": null
}
]
}
]
I try something like:
for(let i=0;i<quizAbertos.length;i++){
if(i+1 === quizAbertos.length){
break;
}else{
if(bookUnitIdInformacoes[i].book_id === bookUnitIdInformacoes[i+1].book_id){
idioma[i].quiz = bookUnitIdInformacoes[i]
}
}
But i'm getting wrong json..
#Edit:
Actually i'm trying something like:
let book_id
let i_book_id = 0
let i_mudou_book_id = 0;
for(let i=0;i<bookUnitIdInformacoes.length;i++){
if(bookUnitIdInformacoes[i+1] === undefined){
book_id = bookUnitIdInformacoes[bookUnitIdInformacoes.length-1].book_id
}else{
if(bookUnitIdInformacoes[i].book_id === bookUnitIdInformacoes[i+1].book_id){
i_mudou_book_id++
}
}
}
idioma[0].quiz = bookUnitIdInformacoes.splice(0,i_mudou_book_id+1)
idioma[1].quiz = bookUnitIdInformacoes
but this way if i have more than 2 length i will be have problems and if i have only one length i will be too have problems.
First, you can group the bookUnitIdInfomacoes by book_id. This can be done by reducing the array into an object of key => value pairs as book_id => array of books:
{
"33": [
{
"id": 5,
"book_id": 33,
...
},
{
"id": 6,
"book_id": 33,
...
},
{
"id": 7,
"book_id": 33,
...
}
],
"39": [
{
"id": 8,
"book_id": 39,
...
}
]
}
Then, using Object.values will allow us to retrieve only the values of this groupedObj object. This will give us:
[
[
{
"id": 5,
"book_id": 33,
...
},
{
"id": 6,
"book_id": 33,
...
},
{
"id": 7,
"book_id": 33,
...
}
],
[
{
"id": 8,
"book_id": 39,
...
}
]
]
Lastly, we will have to map idiomas and add the corresponding book group into a new property called quizz. For a given idioma, we know its position in idiomas thanks to the second argument of map: i. We can simply do grouped[i] to get the corresponding group of books.
const groupedObj = bookUnitIdInformacoes.reduce((grouped, info) => {
grouped[info.book_id] = grouped[info.book_id] || [];
grouped[info.book_id].push(info);
return grouped;
}, {});
const grouped = Object.values(groupedObj);
const result = idiomas.map((idioma, i) => ({
...idioma,
quizz: grouped[i]
}));
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script>const bookUnitIdInformacoes=[{id:5,book_id:33,unit:1,sequence:1,description:"UNIT_01_GRAMMAR",qt_question:5,status:!1,user_id:1,created_at:"2019-12-27 08:11:21",updated_at:"2019-12-30 14:54:12",miniature:null},{id:6,book_id:33,unit:1,sequence:2,description:"UNIT_01_VOCABULARY",qt_question:5,status:!1,user_id:1,created_at:"2019-12-27 08:11:39",updated_at:"2019-12-27 08:11:39",miniature:null},{id:7,book_id:33,unit:2,sequence:1,description:"UNIT_02_GRAMMAR",qt_question:5,status:!1,user_id:1,created_at:"2019-12-27 08:11:46",updated_at:"2019-12-27 08:11:46",miniature:null},{id:8,book_id:39,unit:1,sequence:1,description:"UNIT_01_GRAMMAR",qt_question:5,status:!1,user_id:1,created_at:"2019-12-30 11:07:09",updated_at:"2019-12-30 15:03:50",miniature:null}],idiomas=[{id:13,code:"ING-NOT-2019",description:"Inglês Noturno 2019",start_date:"2019-12-30T03:00:00.000Z",end_date:"2019-12-31T03:00:00.000Z",period:"Noturno",language:"Inglês",status:!1,user_id:1,created_at:"2019-12-30 10:04:47",updated_at:"2020-01-05 16:08:00",language_substring:"US"},{id:14,code:"ESP-MAN-2019",description:"Espanhol manhã 2019",start_date:"2019-12-30T03:00:00.000Z",end_date:"2019-12-31T03:00:00.000Z",period:"Manhã",language:"Espanhol",status:!1,user_id:1,created_at:"2019-12-30 11:06:44",updated_at:"2019-12-30 11:06:44",language_substring:null}];</script>

Count items in vue object

I like to know how many items have the value material_delivery is 1?
I like to do it in a vue method.
[{
"id": 43,
"uuid": "c92421d0-71cc-433d-b7b5-fc6c91c2a3a4",
"project_id": 8,
"name": "Konstruktionsfreigabe",
"due_date": "2019-10-18",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-31 13:09:41"
}, {
"id": 44,
"uuid": "a063964b-f8fc-4c28-9055-09ed5fc4b8dd",
"project_id": 8,
"name": "Material",
"due_date": "2019-12-13",
"material_delivery": 1,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-31 13:06:37"
}, {
"id": 45,
"uuid": "7de3410f-82c2-4b30-8e69-56906b16da4b",
"project_id": 8,
"name": "Montageende",
"due_date": "2019-12-16",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-30 16:43:04"
}, {
"id": 46,
"uuid": "8b034697-543c-46f6-a5be-104011700fb9",
"project_id": 8,
"name": "Lieferung",
"due_date": "2020-01-25",
"material_delivery": 1,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-31 13:57:16"
}, {
"id": 47,
"uuid": "ec1101cf-97cc-4eed-a2c6-0685b7cc073b",
"project_id": 8,
"name": "Abnahme",
"due_date": "2020-03-05",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-30 16:43:04"
}, {
"id": 48,
"uuid": "deb7324a-64f2-4e1c-a358-87fdb95430ea",
"project_id": 8,
"name": "Rechnung",
"due_date": "2020-04-14",
"material_delivery": 0,
"closed_at": null,
"closed_from": null,
"deleted_at": null,
"created_at": "2019-12-30 16:43:04",
"updated_at": "2019-12-30 16:43:04"
}]
I tried with filter but no success.
Finally, I like to know, if there are more than one item with the value 1 in material_delivery
There are many ways to do this. You could use filter, like you initially tried:
methods: {
countMaterialDelivery(array) {
return array.filter(item => item.material_delivery === 1).length;
}
}

Categories