Related
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()
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>
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;
}
}
just need to get a specific count of a json data
here's the sample json format:
{
"tickets": [
{
"url": "https://asd.zendesk.com/api/v2/tickets/1.json",
"id": 1,
"external_id": null,
"via": {
"channel": "sample_ticket",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2017-04-25T05:56:36Z",
"updated_at": "2017-04-25T05:56:36Z",
"status": "open",
},
{
"url": "https://asd.zendesk.com/api/v2/tickets/2.json",
"id": 2,
"external_id": null,
"via": {
"channel": "sample_ticket",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2017-04-25T05:56:36Z",
"updated_at": "2017-04-25T05:56:36Z",
"status": "close",
},
{
"url": "https://asd.zendesk.com/api/v2/tickets/2.json",
"id": 2,
"external_id": null,
"via": {
"channel": "sample_ticket",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2017-04-25T05:56:36Z",
"updated_at": "2017-04-25T05:56:36Z",
"status": "solve",
}
]
}
i want to count how many open, close and solve status using php or ajax or both... need the data auto load also... so we don't need to refresh..
Check this out:
var jsonObject = {
"tickets": [
{
"url": "https://asd.zendesk.com/api/v2/tickets/1.json",
"id": 1,
"external_id": null,
"via": {
"channel": "sample_ticket",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2017-04-25T05:56:36Z",
"updated_at": "2017-04-25T05:56:36Z",
"status": "open",
},
{
"url": "https://asd.zendesk.com/api/v2/tickets/1.json",
"id": 1,
"external_id": null,
"via": {
"channel": "sample_ticket",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2017-04-25T05:56:36Z",
"updated_at": "2017-04-25T05:56:36Z",
"status": "open",
},
{
"url": "https://asd.zendesk.com/api/v2/tickets/2.json",
"id": 2,
"external_id": null,
"via": {
"channel": "sample_ticket",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2017-04-25T05:56:36Z",
"updated_at": "2017-04-25T05:56:36Z",
"status": "close",
},
{
"url": "https://asd.zendesk.com/api/v2/tickets/2.json",
"id": 2,
"external_id": null,
"via": {
"channel": "sample_ticket",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2017-04-25T05:56:36Z",
"updated_at": "2017-04-25T05:56:36Z",
"status": "solve",
}
]
};
var open = 0, close = 0; solve = 0;
for( var i = 0; i < jsonObject.tickets.length; i++)
{
if(jsonObject.tickets[i].status == "open")
{
open++;
}
else if(jsonObject.tickets[i].status == "close")
{
close++;
}
else if(jsonObject.tickets[i].status == "solve")
{
solve++;
}
}
console.log(open, close, solve);
How do I remove the object in my array subBrands that is nested inside another array where the id property of an object is = to 31. I'm trying to get the whole parent array back without that subBrand removed.
Array is:
[
{
"id": 10,
"name": "Parent Brand 1",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:55:51",
"updated_at": "2017-02-02 09:55:51",
"subBrands": [
{
"id": 31,
"name": "Sub Brand 6",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:49",
"updated_at": "2017-02-02 11:42:02"
},
{
"id": 32,
"name": "Sub Brand 7",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:57",
"updated_at": "2017-02-02 11:42:18"
},
{
"id": 33,
"name": "Sub Brand 8",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:04",
"updated_at": "2017-02-02 11:42:34"
},
{
"id": 34,
"name": "Sub Brand 9",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:39",
"updated_at": "2017-02-02 11:42:43"
},
{
"id": 35,
"name": "Sub Brand 10",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:46",
"updated_at": "2017-02-02 11:42:52"
},
{
"id": 36,
"name": "Sub Brand 4",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:43:53",
"updated_at": "2017-02-02 11:43:53"
}
]
},
{
"id": 12,
"name": "Parent Brand 2",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:56:16",
"updated_at": "2017-02-02 09:56:16",
"subBrands": []
},
{
"id": 16,
"name": "Brand no children",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 10:37:40",
"updated_at": "2017-02-02 10:37:40",
"subBrands": []
},
{
"id": 37,
"name": "Whoops brand",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:44:10",
"updated_at": "2017-02-02 11:44:10",
"subBrands": []
}
]
What I'm trying to get is:
[
{
"id": 10,
"name": "Parent Brand 1",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:55:51",
"updated_at": "2017-02-02 09:55:51",
"subBrands": [
{
"id": 32,
"name": "Sub Brand 7",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:24:57",
"updated_at": "2017-02-02 11:42:18"
},
{
"id": 33,
"name": "Sub Brand 8",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:04",
"updated_at": "2017-02-02 11:42:34"
},
{
"id": 34,
"name": "Sub Brand 9",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:39",
"updated_at": "2017-02-02 11:42:43"
},
{
"id": 35,
"name": "Sub Brand 10",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:25:46",
"updated_at": "2017-02-02 11:42:52"
},
{
"id": 36,
"name": "Sub Brand 4",
"parent": 10,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:43:53",
"updated_at": "2017-02-02 11:43:53"
}
]
},
{
"id": 12,
"name": "Parent Brand 2",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 09:56:16",
"updated_at": "2017-02-02 09:56:16",
"subBrands": []
},
{
"id": 16,
"name": "Brand no children",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 10:37:40",
"updated_at": "2017-02-02 10:37:40",
"subBrands": []
},
{
"id": 37,
"name": "Whoops brand",
"parent": null,
"author": 1,
"deleted_at": null,
"created_at": "2017-02-02 11:44:10",
"updated_at": "2017-02-02 11:44:10",
"subBrands": []
}
]
I'm open to using underscores. The closest I'm come is:
var brands = _.filter(brands, function(n) {
return _.some(n.subBrands, function(subBrand){
return subBrand.id != brand.id;
});
});
But that removes the arrays that don't contain a subBrand with an id of 31. So it's not very close to what I need.
Cheers!
var arr = [{"id":10,"name":"Parent Brand 1","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 09:55:51","updated_at":"2017-02-02 09:55:51","subBrands":[{"id":31,"name":"Sub Brand 6","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:24:49","updated_at":"2017-02-02 11:42:02"},{"id":32,"name":"Sub Brand 7","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:24:57","updated_at":"2017-02-02 11:42:18"},{"id":33,"name":"Sub Brand 8","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:04","updated_at":"2017-02-02 11:42:34"},{"id":34,"name":"Sub Brand 9","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:39","updated_at":"2017-02-02 11:42:43"},{"id":35,"name":"Sub Brand 10","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:25:46","updated_at":"2017-02-02 11:42:52"},{"id":36,"name":"Sub Brand 4","parent":10,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:43:53","updated_at":"2017-02-02 11:43:53"}]},{"id":12,"name":"Parent Brand 2","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 09:56:16","updated_at":"2017-02-02 09:56:16","subBrands":[]},{"id":16,"name":"Brand no children","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 10:37:40","updated_at":"2017-02-02 10:37:40","subBrands":[]},{"id":37,"name":"Whoops brand","parent":null,"author":1,"deleted_at":null,"created_at":"2017-02-02 11:44:10","updated_at":"2017-02-02 11:44:10","subBrands":[]}];
var id = prompt("Id of subbrands to remove: ");
arr.forEach(function(o) {
o.subBrands = o.subBrands.filter(s => s.id != id);
});
console.log(arr);
You could iterate the parent part, and the children and if found splice the object.
var data = [{ id: 10, name: "Parent Brand 1", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 09:55:51", updated_at: "2017-02-02 09:55:51", subBrands: [{ id: 31, name: "Sub Brand 6", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:24:49", updated_at: "2017-02-02 11:42:02" }, { id: 32, name: "Sub Brand 7", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:24:57", updated_at: "2017-02-02 11:42:18" }, { id: 33, name: "Sub Brand 8", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:25:04", updated_at: "2017-02-02 11:42:34" }, { id: 34, name: "Sub Brand 9", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:25:39", updated_at: "2017-02-02 11:42:43" }, { id: 35, name: "Sub Brand 10", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:25:46", updated_at: "2017-02-02 11:42:52" }, { id: 36, name: "Sub Brand 4", parent: 10, author: 1, deleted_at: null, created_at: "2017-02-02 11:43:53", updated_at: "2017-02-02 11:43:53" }] }, { id: 12, name: "Parent Brand 2", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 09:56:16", updated_at: "2017-02-02 09:56:16", subBrands: [] }, { id: 16, name: "Brand no children", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 10:37:40", updated_at: "2017-02-02 10:37:40", subBrands: [] }, { id: 37, name: "Whoops brand", parent: null, author: 1, deleted_at: null, created_at: "2017-02-02 11:44:10", updated_at: "2017-02-02 11:44:10", subBrands: [] }];
data.some(function (a) {
return a.subBrands.some(function (b, i, bb) {
if (b.id === 31) {
bb.splice(i, 1);
return true;
}
});
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I nest two forEach loops and return when the item has been found and removed:
let items = [{id: 1, subItems: [{id: 1}, {id: 2}]}];
const subItemToBeRemovedId = 1;
items.forEach((item) => item.subItems.forEach((subItem, index) => {
if (subItem.id === subItemToBeRemovedId) {
return item.subItems.splice(index, 1);
}
}));
console.log(items);
foreach seems to work:
var brands=[{id:10,name:"Parent Brand 1",parent:null,author:1,deleted_at:null,created_at:"2017-02-02 09:55:51",updated_at:"2017-02-02 09:55:51",subBrands:[{id:31,name:"Sub Brand 6",parent:10,author:1,deleted_at:null,created_at:"2017-02-02 11:24:49",updated_at:"2017-02-02 11:42:02"},{id:32,name:"Sub Brand 7",parent:10,author:1,deleted_at:null,created_at:"2017-02-02 11:24:57",updated_at:"2017-02-02 11:42:18"},{id:33,name:"Sub Brand 8",parent:10,author:1,deleted_at:null,created_at:"2017-02-02 11:25:04",updated_at:"2017-02-02 11:42:34"},{id:34,name:"Sub Brand 9",parent:10,author:1,deleted_at:null,created_at:"2017-02-02 11:25:39",updated_at:"2017-02-02 11:42:43"},{id:35,name:"Sub Brand 10",parent:10,author:1,deleted_at:null,created_at:"2017-02-02 11:25:46",updated_at:"2017-02-02 11:42:52"},{id:36,name:"Sub Brand 4",parent:10,author:1,deleted_at:null,created_at:"2017-02-02 11:43:53",updated_at:"2017-02-02 11:43:53"}]},{id:12,name:"Parent Brand 2",parent:null,author:1,deleted_at:null,created_at:"2017-02-02 09:56:16",updated_at:"2017-02-02 09:56:16",subBrands:[]},{id:16,name:"Brand no children",parent:null,author:1,deleted_at:null,created_at:"2017-02-02 10:37:40",updated_at:"2017-02-02 10:37:40",subBrands:[]},{id:37,name:"Whoops brand",parent:null,author:1,deleted_at:null,created_at:"2017-02-02 11:44:10",updated_at:"2017-02-02 11:44:10",subBrands:[]}];
brands.forEach(function(brand) {
brand.subBrands = brand.subBrands.filter(function(subBrand){
return subBrand.id != 31;
})
});
console.log(brands);
If you just need to look into the "subBrands" property of each item of your array, and you are using underscore, this works:
var myBrand = _.each(brands, function(brand) {
brand.subBrands = _.filter(brand.subBrands, function(subBrand) {
return subBrand.id != 31;
});
});
The jQuery way
function removeById(data, id){
$(data).each(function(i, e){
if(e.subBrands.length > 0){
$(e.subBrands).each(function(_i, _e){
if(_e.id == id){
e.subBrands.splice(_i,1);
return false;
}
});
}
});
return data;
}
console.log(removeById(data,32))
this function will return your whole data array without the specifiec id object
You can use for..of loop to iterate each subBrands array, Array.prototype.splice() to remove object having id 31 from array.
for (let {subBrands} of data) {
let n = 0;
for (let {id} of subBrands) {
if (id === 31) {
subBrands.splice(n, 1);
break;
}
++n;
}
}