class Characteristics(models.Model):
name = models.CharField('Наименование', max_length=50, unique=True)
const comments = {"reviews": [
{
"id": 1,
"children": [
{
"id": 2,
"children": [
{
"id": 3,
"children": [
{
"id": 4,
"children": [],
"user": {
"id": 1,
"username": "Admin",
"avatar": "http://127.0.0.1:8000/media/developers/default.jpg"
},
"text": "Ch - 3",
"uploaded_time": "2020-07-30T12:24:52.414582+03:00",
"parent": 3
}
],
"user": {
"id": 1,
"username": "Admin",
"avatar": "http://127.0.0.1:8000/media/developers/default.jpg"
},
"text": "Ch - 2",
"uploaded_time": "2020-07-28T18:20:39.887167+03:00",
"parent": 2
}
],
"user": {
"id": 1,
"username": "Admin",
"avatar": "http://127.0.0.1:8000/media/developers/default.jpg"
},
"text": "Ch - 1",
"uploaded_time": "2020-07-28T18:20:21.004694+03:00",
"parent": 1
}
],
"user": {
"id": 1,
"username": "Admin",
"avatar": "http://127.0.0.1:8000/media/developers/default.jpg"
},
"text": "Text",
"uploaded_time": "2020-07-28T18:19:22.644345+03:00",
"parent": null
}
],}
function countComments(obj){
let total = 0;
count(obj);
function count(obj){
for(let i in obj){
if(obj[i].hasOwnProperty('children')){
count(obj[i].children);
}
total++;
}
}
return total;
}
let total = countComments(comments.reviews);
console.log('Всего комментов: '+ total);