Object value loop - javascript

Im trying to loop with an if statement, where the if statement should skip the null value. But some how it won't recognize the null.
My array object value has this
[ { "id": 17, "match_id": 17, "dktournament_id": 10, "seed_match": 1, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:53", "score": { "id": 17, "playerOne": "Knud", "playerTwo": "Weise", "setOne": 3, "setTwo": 0, "created_at": "2020-02-11 21:37:53", "updated_at": "2020-02-11 21:38:21" } }, { "id": 18, "match_id": 18, "dktournament_id": 10, "seed_match": 2, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:39:07", "score": { "id": 18, "playerOne": "Hans", "playerTwo": "Khan", "setOne": 0, "setTwo": 3, "created_at": "2020-02-11 21:39:07", "updated_at": "2020-02-11 21:39:42" } }, { "id": 19, "match_id": 19, "dktournament_id": 10, "seed_match": 3, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:44:12", "score": { "id": 19, "playerOne": "Preben", "playerTwo": "Gertrud", "setOne": 1, "setTwo": 0, "created_at": "2020-02-11 21:44:12", "updated_at": "2020-02-21 12:24:39" } }, { "id": 20, "match_id": 20, "dktournament_id": 10, "seed_match": 4, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:58:02", "score": { "id": 20, "playerOne": "Ingvard", "playerTwo": "Oscar", "setOne": 0, "setTwo": 0, "created_at": "2020-02-11 21:58:02", "updated_at": "2020-02-11 21:58:02" } }, { "id": 21, "match_id": null, "dktournament_id": 10, "seed_match": 5, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 22, "match_id": null, "dktournament_id": 10, "seed_match": 6, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 23, "match_id": null, "dktournament_id": 10, "seed_match": 7, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 24, "match_id": null, "dktournament_id": 10, "seed_match": 8, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null } ]
My method is this
totalsetsTeamOne(){
let i;
let SetOneTotal = 0;
for (i = 0; i < 8; i++) {
if(this.seedmatches[i]!= null){
SetOneTotal += this.seedmatches[i].score.setOne;
}
}
this.setOneTotal = SetOneTotal;
console.log(SetOneTotal)
},
My error is this
[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'setOne' of null"

Your if statement should be
if(this.seedmatches[i] && this.seedmatches[i].score){
SetOneTotal += this.seedmatches[i].score.setOne;
}
Because you are only checking if there are values in this.seedmatches[i]. But what if there are values in this.seedmatches[i] but this.seedmatches[i].score is null? Therefore, we need to check thoroughly if this.seedmatches[i].score is null also.

Because "id": 21 and other items have "score":null

You are looking if this.seedmatches[i] but not if this.seedmatches[i].score is null

This code will work..Try this...
totalsetsTeamOne() {
let i;
let SetOneTotal = 0;
for (i = 0; i < 8; i++) {
if (this.seedmatches[i] && this.seedmatches[i].score !== null) {
SetOneTotal += seedmatches[i].score.setOne;
}
}
this.setOneTotal = SetOneTotal;
console.log(SetOneTotal)
},

Here are two versions of the calculation, using expressions.
The second uses the optional chaining operator.
const data = [ { "id": 17, "match_id": 17, "dktournament_id": 10, "seed_match": 1, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:53", "score": { "id": 17, "playerOne": "Knud", "playerTwo": "Weise", "setOne": 3, "setTwo": 0, "created_at": "2020-02-11 21:37:53", "updated_at": "2020-02-11 21:38:21" } }, { "id": 18, "match_id": 18, "dktournament_id": 10, "seed_match": 2, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:39:07", "score": { "id": 18, "playerOne": "Hans", "playerTwo": "Khan", "setOne": 0, "setTwo": 3, "created_at": "2020-02-11 21:39:07", "updated_at": "2020-02-11 21:39:42" } }, { "id": 19, "match_id": 19, "dktournament_id": 10, "seed_match": 3, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:44:12", "score": { "id": 19, "playerOne": "Preben", "playerTwo": "Gertrud", "setOne": 1, "setTwo": 0, "created_at": "2020-02-11 21:44:12", "updated_at": "2020-02-21 12:24:39" } }, { "id": 20, "match_id": 20, "dktournament_id": 10, "seed_match": 4, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:58:02", "score": { "id": 20, "playerOne": "Ingvard", "playerTwo": "Oscar", "setOne": 0, "setTwo": 0, "created_at": "2020-02-11 21:58:02", "updated_at": "2020-02-11 21:58:02" } }, { "id": 21, "match_id": null, "dktournament_id": 10, "seed_match": 5, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 22, "match_id": null, "dktournament_id": 10, "seed_match": 6, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 23, "match_id": null, "dktournament_id": 10, "seed_match": 7, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 24, "match_id": null, "dktournament_id": 10, "seed_match": 8, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null } ]
const setOneTotal1 = (arr) =>
arr.reduce((total, { score }) =>
((score && (total += score.setOne)), total), 0)
console.log(setOneTotal1(data))
const setOneTotal2 = (arr) =>
arr.reduce((total, {score}, _, __, s1 = score?.setOne) =>
(s1 ? total + s1 : total), 0)
console.log(setOneTotal2(data))

Related

How to get specific field from nested table relation in adonisjs

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()

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;
}
}

Tree massiv get all the links of the elements

I have and object literal that is essentially a tree that does not have a fixed number of levels.
How can I search for a tree for a particular node, and then return this node with connection parameters when it is found in a spectacular javascript manner?
var data = [{
title: 'topNode',
id: 1,
children: [{
title: 'node1',
id: 2,
children: [{
title: 'randomNode_1',
id: 3,
},
{
title: 'node2',
id: 4,
children: [{
title: 'randomNode_2',
id: 5,
children: [{
title: 'node2',
id: 1111,
children: [{
title: 'randomNode_3',
id: 1232,
}]
}]
}]
}
]
}]
}];
tree build   what should be done
tree built from api
serializeGoals: state => type => {
const _goals = JSON.parse(JSON.stringify(state.goals))
return {
title: 'Стратегия',
expand: true,
forst: true,
children: _goals.filter((item) => {
if (item.type !== null && type.some(typeID => typeID === item.type.id)) {
item.shadow = true
} else if (item.type !== null && type.length > 0) {
item.shadow = false
item.type.color = '#ECECEC'
}
item.children = _goals.filter((children) => {
if (children.parent_id === item.id) {
if (children.type !== null && type.some(typeID => typeID === children.type.id)) {
children.shadow = true
} else if (children.type !== null && type.length > 0) {
children.type.color = '#ECECEC'
children.shadow = false
}
return true
}
return false
})
return item.parent_id === null
})
}
}
Get api server JSON array
parent_id - element parent
[{
"id": 5,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 5",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 7,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 7",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 9,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 9",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 11,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 11",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 13,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 13",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 15,
"parent_id": null,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 15",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 17,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 17",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 19,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 19",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 20,
"parent_id": 11,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 20",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 21,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 21",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 23,
"parent_id": null,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 23",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 25,
"parent_id": null,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 25",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 26,
"parent_id": 15,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 26",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 27,
"parent_id": null,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 27",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 28,
"parent_id": 7,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 28",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 29,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 29",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 31,
"parent_id": null,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 31",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}, {
"id": 33,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 33",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 34,
"parent_id": 31,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 34",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 35,
"parent_id": null,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 35",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 36,
"parent_id": 34,
"responsible_id": null,
"type_id": 1,
"title": "Тестовая цель - 36",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 1, "title": "Финансы", "color": "#ffc82c"}
}, {
"id": 37,
"parent_id": null,
"responsible_id": null,
"type_id": 4,
"title": "Тестовая цель - 37",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 4, "title": "Персонал", "color": "#6190e8"}
}, {
"id": 39,
"parent_id": null,
"responsible_id": null,
"type_id": 3,
"title": "Тестовая цель - 39",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 3, "title": "Процессы", "color": "#13ce66"}
}, {
"id": 40,
"parent_id": 34,
"responsible_id": null,
"type_id": 2,
"title": "Тестовая цель - 40",
"description": null,
"period": 2018,
"weight": null,
"responsible": null,
"type": {"id": 2, "title": "Клиенты", "color": "#ff4949"}
}]
I would use a recursive function to iterate through the tree and return the node when the expected one is found (I assumed the title is the search criteria) or return null if none was found.
In example :
var data = [
{
title: 'topNode',
id: 1,
children: [
{
title: 'node1',
id: 2,
children: [
{
title: 'randomNode_1',
id: 3,
},
{
title: 'node2',
id: 4,
children: [
{
title: 'randomNode_2',
id: 5,
children: [
{
title: 'node2',
id: 1111,
children: [
{
title: 'randomNode_3',
id: 1232,
}]
}]
}]
}]
}]
}];
function FindSpecificData(node, titleToFind)
{
let newNode = null;
for (let i = 0; i < node.length; ++i)
{
if (node[i].title == titleToFind)
newNode = node[i];
else if ('children' in node[i])
newNode = FindSpecificData(node[i].children, titleToFind);
if (newNode != null)
return (newNode);
}
return (null);
}
console.log(FindSpecificData(data, 'randomNode_2'));
console.log(FindSpecificData(data, 'randomNode_1'));
console.log(FindSpecificData(data, 'missing node'));
Try this simple method to find out your node
function getNode(node) {
if (node.title === 'randomNode_3') {
return true;
} else if (!node.children && !node.id) {
return false;
} else {
if (node.children) {
for (var i = 0; i < node.children.length; i++) {
getNode(node.children[i])
}
}
}
}
getNode(data[0])

Categories