Good morning all,
I've been working on a small project to build a dynamic treant.js tree chart. To achieve this i have based my code on the collapsable example using the JSON method.
Unfortunately the JSON within the script isnt exactly perfect JSON which is making my life particularly difficult.
I have written a piece of script which creates the required JSON as a string which when I write to the window and copy into the collapsable.js the chart is drawn perfectly.
An example can be seen here;
{chart: {container: "#collapsable-example",animateOnInit: true,node: {collapsable: true},animation: {nodeAnimation: "easeOutBounce",nodeSpeed: 700,connectorsAnimation: "bounce",connectorsSpeed: 700}},nodeStructure: { "id": 1, "parent": 0, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 2, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 4, "parent": 2, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 3, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 5, "parent": 3, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 6, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] }, { "id": 7, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }
what im struggling with is after I've built that string converting it to an object that treant.js likes.
for example
var chart_config = {chart: {container: "#collapsable-example",animateOnInit: true,node: {collapsable: true},animation: {nodeAnimation: "easeOutBounce",nodeSpeed: 700,connectorsAnimation: "bounce",connectorsSpeed: 700}},nodeStructure: { "id": 1, "parent": 0, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 2, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 4, "parent": 2, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 3, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 5, "parent": 3, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 6, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] }, { "id": 7, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }
When the JSON is copied and pasted from the result of the code that generated it works absolutley fine but.....
var tree = eval({chart: {container: "#collapsable-example",animateOnInit: true,node: {collapsable: true},animation: {nodeAnimation: "easeOutBounce",nodeSpeed: 700,connectorsAnimation: "bounce",connectorsSpeed: 700}},nodeStructure: { "id": 1, "parent": 0, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 2, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 4, "parent": 2, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 3, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [ { "id": 5, "parent": 3, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] }, { "id": 6, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] }, { "id": 7, "parent": 1, "text": { "name": "Tony Obrien", "Title": "Managing Director" }, "children": [] } ] })
chart_config = tree
This doesnt work. I get an unexpected token error. I have tried JSON.parse to no avail either. Does anyone have any ideas?
Your unexpected token error is likely from the eval call. Notice that it takes a string representing js code as an argument. You are giving it a js object, so the first '{' is likely the 'unexpected token'. Trying removing the eval wrapper and I bet it works.
Related
I have a deeply nested array like below. I want to flat the structure.
data = [
{
"id": 4321,
"name": "category1",
"parentId": null,
"children": [
{
"id": 1234,
"name": "category1",
"parentId": 4321,
"children": [
{
"id": 8327548,
"name": "001",
"parentId": 1234
},
{
"id": 8327549,
"name": "002",
"parentId": 1234
},
]
},
{
"id": 6786,
"name": "Associations",
"parentId": 4321
},
{
"id": 8262439,
"name": "category1",
"parentId": 4321
},
{
"id": 8245,
"name": "Rights",
"parentId": 4321,
"children": [
{
"id": 2447,
"name": "Organizations",
"parentId": 8245
},
{
"id": 9525,
"name": "Services",
"parentId": 8245
},
{
"id": 8448,
"name": "Organizations",
"parentId": 8245
}
]
},
{
"id": 8262446,
"name": "Women's Rights",
"parentId": 4321
}
]
},
{
"id": 21610,
"name": "Agriculture",
"parentId": null,
"children": [
{
"id": 3302,
"name": "categoryABC",
"parentId": 21610,
"children": [
{
"id": 85379,
"name": "categoryABC - General",
"parentId": 3302
},
{
"id": 85380,
"name": "categoryABC Technology",
"parentId": 3302
}
]
},
{
"id": 8303,
"name": "Fungicides",
"parentId": 21610,
"children": [
{
"id": 8503,
"name": "Fungicides - General",
"parentId": 8303
}
]
},
]
},
];
Expected output
output = [
{
"id": 8327548,
"name": "001",
"parentId": 1234
},
{
"id": 8327549,
"name": "002",
"parentId": 1234
},
...OTHER OBJECTS....
]
What I have tried so far. This is not pushing the inner children items.
function flat(array) {
var result = [];
array.forEach(function (a) {
result.push(a);
if (Array.isArray(a.children)) {
result = result.concat(flat(a.children));
}
});
return result;
}
let results = flat(data)
console.log("test", results)
Stack Snippet:
const data = [
{
"id": 4321,
"name": "category1",
"parentId": null,
"children": [
{
"id": 1234,
"name": "category1",
"parentId": 4321,
"children": [
{
"id": 8327548,
"name": "001",
"parentId": 1234
},
{
"id": 8327549,
"name": "002",
"parentId": 1234
},
]
},
{
"id": 6786,
"name": "Associations",
"parentId": 4321
},
{
"id": 8262439,
"name": "category1",
"parentId": 4321
},
{
"id": 8245,
"name": "Rights",
"parentId": 4321,
"children": [
{
"id": 2447,
"name": "Organizations",
"parentId": 8245
},
{
"id": 9525,
"name": "Services",
"parentId": 8245
},
{
"id": 8448,
"name": "Organizations",
"parentId": 8245
}
]
},
{
"id": 8262446,
"name": "Women's Rights",
"parentId": 4321
}
]
},
{
"id": 21610,
"name": "Agriculture",
"parentId": null,
"children": [
{
"id": 3302,
"name": "categoryABC",
"parentId": 21610,
"children": [
{
"id": 85379,
"name": "categoryABC - General",
"parentId": 3302
},
{
"id": 85380,
"name": "categoryABC Technology",
"parentId": 3302
}
]
},
{
"id": 8303,
"name": "Fungicides",
"parentId": 21610,
"children": [
{
"id": 8503,
"name": "Fungicides - General",
"parentId": 8303
}
]
},
]
},
];
function flat(array) {
var result = [];
array.forEach(function (a) {
result.push(a);
if (Array.isArray(a.children)) {
result = result.concat(flat(a.children));
}
});
return result;
}
let results = flat(data)
console.log("test", results)
Can someone help me please?
I'd suggest a recursive approach, walking through the input structure and pushing each object found to the result array.
data = [ { "id": 4321, "name": "category1", "parentId": null, "children": [ { "id": 1234, "name": "category1", "parentId": 4321, "children": [ { "id": 8327548, "name": "001", "parentId": 1234 }, { "id": 8327549, "name": "002", "parentId": 1234 }, ] }, { "id": 6786, "name": "Associations", "parentId": 4321 }, { "id": 8262439, "name": "category1", "parentId": 4321 }, { "id": 8245, "name": "Rights", "parentId": 4321, "children": [ { "id": 2447, "name": "Organizations", "parentId": 8245 }, { "id": 9525, "name": "Services", "parentId": 8245 }, { "id": 8448, "name": "Organizations", "parentId": 8245 } ] }, { "id": 8262446, "name": "Women's Rights", "parentId": 4321 } ] }, { "id": 21610, "name": "Agriculture", "parentId": null, "children": [ { "id": 3302, "name": "categoryABC", "parentId": 21610, "children": [ { "id": 85379, "name": "categoryABC - General", "parentId": 3302 }, { "id": 85380, "name": "categoryABC Technology", "parentId": 3302 } ] }, { "id": 8303, "name": "Fungicides", "parentId": 21610, "children": [ { "id": 8503, "name": "Fungicides - General", "parentId": 8303 } ] }, ] }, ];
function flat(input, result = []) {
let newObj = null;
for(let k in input) {
if (typeof(input[k]) === 'object') {
flat(input[k], result);
} else {
if (!newObj) {
newObj = {};
result.push(newObj);
}
newObj[k] = input[k];
}
}
return result;
}
console.log(flat(data))
.as-console-wrapper { max-height: 100% !important; top: 0; }
You need to delete a.children from the original array:
if (Array.isArray(a.children)) {
result = result.concat(flat(a.children));
delete a.children;
}
(As #T.J.Crowde suggested, I left the snippet with a smaller array)
const data = [
{
"id": 4321,
"name": "category1",
"parentId": null,
"children": [
{
"id": 1234,
"name": "category1",
"parentId": 4321,
"children": [
{
"id": 8327548,
"name": "001",
"parentId": 1234
},
{
"id": 8327549,
"name": "002",
"parentId": 1234
},
]
},
]
},
];
function flat(array) {
var result = [];
array.forEach(function (a) {
result.push(a);
if (Array.isArray(a.children)) {
result = result.concat(flat(a.children));
delete a.children;
}
});
return result;
}
let results = flat(data)
console.log("test", results)
BE Response:- which I am getting from Backend Service
{
"data": {
"type": "AnyType",
"resources": [
{
"id": 1,
"treeId": "1",
"name": "name1",
"description": "description1",
"children": [
{
"id": 3,
"treeId": "1-3",
"name": "subName1",
"description": "subDescription1",
"children": [
{
"id": 6,
"treeId": "1-3-6",
"name": "subSubName1",
"description": "subSubDesc1",
"children": []
}
]
}
]
},
{
"id": 2,
"treeId": "2",
"name": "name2",
"description": "description2",
"children": [
{
"id": 7,
"treeId": "2-7",
"name": "subName2",
"description": "subDescription2",
"children": []
}
]
}
]
}
}
But I need to modify this response to as below on FE
Expected Response:- means I need to join name and description field text to one(in name field ) as below:-
{
"data": {
"type": "AnyType",
"resources": [
{
"id": 1,
"treeId": "1",
"name": "name1-description1",
"description": "description1",
"children": [
{
"id": 3,
"treeId": "1-3",
"name": "subName1-subDescription1",
"description": "subDescription1",
"children": [
{
"id": 6,
"treeId": "1-3-6",
"name": "subSubName1-subSubDesc1",
"description": "subSubDesc1",
"children": []
}
]
}
]
},
{
"id": 2,
"treeId": "2",
"name": "name2-description2",
"description": "description2",
"children": [
{
"id": 7,
"treeId": "2-7",
"name": "subName2-subDescription2",
"description": "subDescription2",
"children": []
}
]
}
]
}
}
there could be n number of children of each object and children can have an array of objects.
What I have done:- I am able to change the very first name but not children name
let resDataArry = [];
let descData: DynamicResource;
response.forEach((x, index) => {
const descName = x.name + ' ' + x.description;
descData = { ...tree.resources[index], name: descName };
resDataArry.push(descData);
});
return resDataArry;
Please help.
You can use nested Array#forEach to access children array and then concatenate the name and description together.
let data = {
"data": {
"type": "AnyType",
"resources": [
{
"id": 1,
"treeId": "1",
"name": "name1",
"description": "description1",
"children": [
{
"id": 3,
"treeId": "1-3",
"name": "subName1",
"description": "subDescription1",
"children": [
{
"id": 6,
"treeId": "1-3-6",
"name": "subSubName1",
"description": "subSubDesc1",
"children": []
}
]
}
]
},
{
"id": 2,
"treeId": "2",
"name": "name2",
"description": "description2",
"children": [
{
"id": 7,
"treeId": "2-7",
"name": "subName2",
"description": "subDescription2",
"children": []
}
]
}
]
}
}
data.data.resources.forEach(function(item){
item.name = item.name + ' ' + item.description;
item.children.forEach(function(child){
child.name = child.name + ' ' + child.description;
});
});
console.log(data);
Alright, I have to this JSON:
https://gist.github.com/pedroapfilho/c1673be24dfdb36133149b4edfc8c2bd
and:
1 - List the categories alphabetically (and filter to show unique values)
2 - Arrange this array in ascending order taking as parameter the sum of the subscription price
Thanks a lot !
Give this a try.
let json = [{
"id": "9b565b11-7311-5b5e-a699-97873dffb361",
"name": "Voice Report",
"description": "Calls reporting and analytics of your calls.",
"categories": ["Voice Analytics", "Reporting", "Optimization"],
"subscriptions": [{
"name": "Trial",
"price": 0
},
{
"name": "Professional",
"price": 3500
}
]
},
{
"id": "470fedc5-489e-5acb-a200-c85adaa18356",
"name": "Power Dialer",
"description": "Auto dialer that will help increase your connect rates and talk time.",
"categories": ["Dialer"],
"subscriptions": [{
"name": "Trial",
"price": 0
},
{
"name": "Professional",
"price": 4500
},
{
"name": "Premium",
"price": 6000
}
]
},
{
"id": "52714d80-e3c4-5593-b9a3-e2ff484be372",
"name": "Smart Text",
"description": "Use SMS to help you communicate with your customers.",
"categories": ["Channels"],
"subscriptions": [{
"name": "Trial",
"price": 0
}]
},
{
"id": "8d68c357-59e6-505a-b0e1-4953196b14df",
"name": "Customer Chat",
"description": "Improve your call center with live chat support.",
"categories": ["Channels"],
"subscriptions": [{
"name": "Trial",
"price": 0
}]
},
{
"id": "dd024ed5-efae-5785-addc-09e592066e5c",
"name": "Report Plus",
"description": "Advanced reporting with custom dashboards.",
"categories": ["Reporting"],
"subscriptions": [{
"name": "Starter",
"price": 2000
},
{
"name": "Plus",
"price": 4500
}
]
},
{
"id": "f820ad5d-32d0-5bb7-aed4-cbc74bcf0b47",
"name": "Screen Share",
"description": "Enable screen sharing between your agents and customers.",
"categories": ["Productivity"],
"subscriptions": [{
"name": "Professional",
"price": 6000
}]
},
{
"id": "7f89f001-9d7d-52f1-82cb-8f44eb1e4680",
"name": "Video Contacts",
"description": "Communicate with your customers and agents using video calls.",
"categories": ["Productivity"],
"subscriptions": [{
"name": "Trial",
"price": 0
},
{
"name": "Professional",
"price": 2500
}
]
},
{
"id": "32be8940-aeb6-5325-ae63-6497772f362a",
"name": "Agent Monitor",
"description": "More tools to monitor your agents activity.",
"categories": ["Productivity", "Management"],
"subscriptions": [{
"name": "Trial",
"price": 0
},
{
"name": "Professional",
"price": 3000
}
]
},
{
"id": "b4e7899b-07ba-55b1-9ed3-c38b878623fe",
"name": "Awesome Calls",
"description": "Tools to optimize your call center with voice analytics.",
"categories": ["Optimization", "Voice Analytics"],
"subscriptions": [{
"name": "Trial",
"price": 0
},
{
"name": "Professional",
"price": 5000
},
{
"name": "Enterprise",
"price": 10000
}
]
},
{
"id": "d8652502-f8f2-5c35-8de5-b9adfebbf4cf",
"name": "Scripted",
"description": "Help your agents communicate with customers using scripts.",
"categories": ["Productivity", "Optimization"],
"subscriptions": [{
"name": "Trial",
"price": 0
},
{
"name": "Professional",
"price": 4000
}
]
}
];
json.sort((a, b) => {
let sumA = a['subscriptions'].reduce((accumulator, current) => {
return accumulator + current['price'];
}, 0);
let sumB = b['subscriptions'].reduce((accumulator, current) => {
return accumulator + current['price'];
}, 0);
return sumA - sumB;
});
let categories = json.reduce((accumulator, current) => {
return accumulator.concat(current['categories']).filter((value, index, self) => {
return self.indexOf(value) === index;
});
}, []).sort();
console.log(json);
console.log(categories);
I have an array with objects and I am trying to figure out how to filter out a few objects which i dont need their. I am trying to filter it out on the basis of the field code . Below is the code I have tried so far which throws an error a.filter is not a function. What could I be doing in this case. Is filter not the correct way to do it .Thank You.
var chartsArray = [
{
"name": "Total Educated",
"code": "Q035001",
"parent": "EDU_ATTAINMENT",
"value": "9900",
"label": "Total Educated",
"children": []
},
{
"name": "Grade Less than 9",
"code": "Q035003",
"parent": "EDU_ATTAINMENT",
"value": "369",
"label": "Grade 9",
"children": []
},
{
"name": "Grade 9 to 12",
"code": "Q035007",
"parent": "EDU_ATTAINMENT",
"value": "595",
"label": "Grade 9 - 12",
"children": []
},
{
"name": "High School",
"code": "Q035011",
"parent": "EDU_ATTAINMENT",
"value": "1174",
"label": "High School",
"children": []
},
{
"name": "Some College",
"code": "Q035012",
"parent": "EDU_ATTAINMENT",
"value": "1904",
"label": "Some College",
"children": []
},
{
"name": "College Degree - Associate's",
"code": "Q035014",
"parent": "EDU_ATTAINMENT",
"value": "436",
"label": "Associate's",
"children": []
},
{
"name": "College Degree - Bachelor's",
"code": "Q035015",
"parent": "EDU_ATTAINMENT",
"value": "2999",
"label": "Bachelor's",
"children": []
},
{
"name": "College Degree - Master's",
"code": "Q035016",
"parent": "EDU_ATTAINMENT",
"value": "1763",
"label": "Master's",
"children": []
},
{
"name": "College - Professional",
"code": "Q035017",
"parent": "EDU_ATTAINMENT",
"value": "413",
"label": "Professional",
"children": []
},
{
"name": "College Degree - Doctorate",
"code": "Q035018",
"parent": "EDU_ATTAINMENT",
"value": "246",
"label": "Doctorate",
"children": []
},
{
"name": "Enrollments (Total Population)",
"code": "EDU_Enrollments",
"parent": "EDU_ATTAINMENT",
"label": "Enrollments (Total Population)",
"children": [
{
"name": "Nursery school/Preschool",
"code": "Q036003",
"parent": "EDU_Enrollments",
"value": "269",
"label": "Nursery school/Preschool",
"children": []
},
{
"name": "Kindergarten/Elementary school",
"code": "Q036006",
"parent": "EDU_Enrollments",
"value": "1156",
"label": "Kindergarten/Elementary school",
"children": []
},
{
"name": "High School",
"code": "Q036015",
"parent": "EDU_Enrollments",
"value": "539",
"label": "High School",
"children": []
},
{
"name": "College/Graduate /Professional school",
"code": "Q036018",
"parent": "EDU_Enrollments",
"value": "1869",
"label": "College/Graduate /Professional school",
"children": []
},
{
"name": "Not Enrolled",
"code": "Q036024",
"parent": "EDU_Enrollments",
"value": "10380",
"label": "Not Enrolled",
"children": []
}
]
},
{
"name": "Percents",
"code": "PCT_EDU_ATTAINMENT",
"parent": "EDU_ATTAINMENT",
"label": "Percents",
"children": [
{
"name": "% Grade Less than 9",
"code": "XQ035003",
"parent": "PCT_EDU_ATTAINMENT",
"value": "3.7231",
"label": "% Grade 9",
"children": []
},
{
"name": "% Grade 9 to 12",
"code": "XQ035007",
"parent": "PCT_EDU_ATTAINMENT",
"value": "6.0112",
"label": "% Grade 9 - 12",
"children": []
},
{
"name": "% High school",
"code": "XQ035011",
"parent": "PCT_EDU_ATTAINMENT",
"value": "11.8622",
"label": "% High school",
"children": []
},
{
"name": "% Some college",
"code": "XQ035012",
"parent": "PCT_EDU_ATTAINMENT",
"value": "19.2332",
"label": "% Some college",
"children": []
},
{
"name": "% College - Associate",
"code": "XQ035014",
"parent": "PCT_EDU_ATTAINMENT",
"value": "4.4073",
"label": "Associate",
"children": []
},
{
"name": "% College - Bachelors",
"code": "XQ035015",
"parent": "PCT_EDU_ATTAINMENT",
"value": "30.2966",
"label": "Bachelors",
"children": []
},
{
"name": "% College - Masters",
"code": "XQ035016",
"parent": "PCT_EDU_ATTAINMENT",
"value": "17.8096",
"label": "Masters",
"children": []
},
{
"name": "% College - Professional",
"code": "XQ035017",
"parent": "PCT_EDU_ATTAINMENT",
"value": "4.168",
"label": "Professional",
"children": []
},
{
"name": "% College - Doctorate",
"code": "XQ035018",
"parent": "PCT_EDU_ATTAINMENT",
"value": "2.4888",
"label": "Doctorate",
"children": []
}
]
}
];
var el=[
"Q035001",
"PCT_EDU_ATTAINMENT"
];
output = chartsArray = chartsArray.map(a => a.filter(function code(o) {
if (!el.includes(o.code)) {
if (o.children) {
o.children = o.children.filter(code);
}
return true;
}
}));
console.log(output)
chartsArray is an array of objects.
When you do chartsArray.map(a => a.filter(...)),
it means that for each object a in the array, invoke filter.
Which is incorrect since objects don't have a filter method.
It seems you meant to use filter on the array, instead of map:
output = chartsArray = chartsArray.filter(function code(o) {
if (!el.includes(o.code)) {
if (o.children) {
o.children = o.children.filter(code);
}
return true;
}
});
You may use recursion to filter recursively:
const filter = [
"Q035001",
"PCT_EDU_ATTAINMENT"
];
function filterBy(arr, filter){
return arr.filter( obj => {
obj.children = filterBy(obj.children, filter);
return !filter.includes(obj code);
});
}
const output = filterBy(chartsArray, filter);
I have a json file returned on my javascript code. The file looks like this :
{
"data": [
{
"id": "594984240522886",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Ducks",
"description": "ducks",
"link": "http://www.facebook.com/album.php?fbid=594984240522886&id=593959083958735&aid=1073741834",
"cover_photo": "594984260522884",
"count": 4,
"type": "normal",
"created_time": "2013-06-13T15:12:22+0000",
"updated_time": "2013-06-13T15:12:40+0000",
"can_upload": false
},
{
"id": "593963787291598",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Profile Pictures",
"link": "http://www.facebook.com/album.php?fbid=593963787291598&id=593959083958735&aid=1073741832",
"cover_photo": "593963797291597",
"count": 1,
"type": "profile",
"created_time": "2013-06-11T16:52:29+0000",
"updated_time": "2013-06-11T16:52:31+0000",
"can_upload": false
},
{
"id": "593963467291630",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Goats",
"description": "goats",
"link": "http://www.facebook.com/album.php?fbid=593963467291630&id=593959083958735&aid=1073741831",
"cover_photo": "593963477291629",
"count": 7,
"type": "normal",
"created_time": "2013-06-11T16:51:56+0000",
"updated_time": "2013-06-11T16:52:02+0000",
"can_upload": false
},
{
"id": "593962700625040",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Dogs",
"description": "dogs",
"link": "http://www.facebook.com/album.php?fbid=593962700625040&id=593959083958735&aid=1073741830",
"cover_photo": "593962710625039",
"count": 10,
"type": "normal",
"created_time": "2013-06-11T16:50:27+0000",
"updated_time": "2013-06-11T16:50:37+0000",
"can_upload": false
},
{
"id": "593961937291783",
"from": {
"id": "593959083958735",
"category": "Community",
"name": "Decoc"
},
"name": "Cows",
"description": "Cows",
"link": "http://www.facebook.com/album.php?fbid=593961937291783&id=593959083958735&aid=1073741829",
"cover_photo": "593961983958445",
"count": 5,
"type": "normal",
"created_time": "2013-06-11T16:48:26+0000",
"updated_time": "2013-06-11T16:49:32+0000",
"can_upload": false
}
],
"paging": {
"cursors": {
"after": "NTkzOTYxOTM3MjkxNzgz",
"before": "NTk0OTg0MjQwNTIyODg2"
}
}
}
I would like to loop inside the "data" and see how many different data elements exist(as you see each element has an id , from , name , description..) . How can i do that with javascript?
You can try the following code:
for(i=0;json.data.length;i++){
var element = json.data[i];
}
or also in this other way:
for (i in json.data) {
if (json.data.hasOwnProperty(i)) {
var element = json.data[i];
}
}