Outputting Object Properties to Console [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I have the following object:
var myObj={
"name": "Chatik",
"type": "public_supergroup",
"id": 9947542893,
"messages": [
{
"id": 1,
"type": "service",
"date": "2019-11-11T21:45:33",
"actor": "Chatik",
"actor_id": 9947542893,
"action": "migrate_from_group",
"title": "Chatik",
"text": ""
},
{
"id": 2,
"type": "message",
"date": "2019-11-11T21:51:22",
"from": "Korney Chukovsky",
"from_id": 4528246494,
"text": "Чому никто не вкатывается?"
},
{
"id": 3,
"type": "message",
"date": "2019-11-11T21:55:13",
"from": "Korney Chukovsky",
"from_id": 4528246494,
"text": "Бля, я даже в своей собственной конфе один."
},
{
"id": 7,
"type": "message",
"date": "2019-11-11T22:05:48",
"from": "Андрей",
"from_id": 4855779304,
"text": "ты откуда?"
}
]};
How do I create a script that would output all "text properties from each "elements" within the curly braces but only if the "from_id" property matches a certain value?
For example if my "from id" value is 4528246494, then "text" value should be outputted to the console, then proceed to check the next "element", if the value is not 4528246494, then skip, etc.

You just have to access message and grab the second element as normal property accessing
var myObj = {
"name": "Chatik",
"type": "public_supergroup",
"id": 9947542893,
"messages": [{
"id": 1,
"type": "service",
"date": "2019-11-11T21:45:33",
"actor": "Chatik",
"actor_id": 9947542893,
"action": "migrate_from_group",
"title": "Chatik",
"text": ""
},
{
"id": 2,
"type": "message",
"date": "2019-11-11T21:51:22",
"from": "Korney Chukovsky",
"from_id": 4528246494,
"text": "Чому никто не вкатывается?"
},
{
"id": 3,
"type": "message",
"date": "2019-11-11T21:55:13",
"from": "Korney Chukovsky",
"from_id": 4528246494,
"text": "Бля, я даже в своей собственной конфе один."
}
]
};
console.log(myObj.messages[1].text)

Related

How can i sort array with objects which have property with another array of objects but not with ES6 [duplicate]

This question already has answers here:
Sort nested array of object in javascript
(4 answers)
Closed 3 years ago.
I have this kind of array with objects. In each object except properties i have another array with objects called "secondary_fields". I need to access the last element in the secondary_fields array (secondary_fields[3]) where i have value of when it is created and after that to sort all of the objects
by the date time based on the value inside for example "2020-01-13 17:42:51";
But not with ES6 because i am using old platform where the ES6 version of Java Script is not supported
let arr =
[
{
"external": false,
"link": "--",
"direct": false,
"display_field": "new best article",
"id": "kb_knowledge:33",
"secondary_fields": [
{
"display_value": "Test Admin",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xx"
},
{
"display_value": "25",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "25"
},
{
"display_value": "2020-01-13 09:44:54",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2020-01-13 17:44:54"
},
{
"display_value": "2020-01-13 09:42:51",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2020-01-13 17:42:51"
}
],
},
{
"external": false,
"link": "--",
"direct": false,
"display_field": "How to connect the iPod to Wi-Fi",
"id": "kb_knowledge:11",
"secondary_fields": [
{
"display_value": "John",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xxnnx"
},
{
"display_value": "16",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "16"
},
{
"display_value": "2019-12-18 08:18:08",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2019-12-18 16:18:08"
},
{
"display_value": "2019-10-21 12:27:22",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2019-10-21 19:27:22"
}
],
}
]
You can make use of the array method's sort method and sort on display_value.
arr.sort(function(a, b) {
return new Date(a.secondary_fields[3].display_value) - new Date(b.secondary_fields[3].display_value)
})
Edit
Or I guess in your case,
return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
let arr =
[
{
"external": false,
"link": "--",
"direct": false,
"display_field": "new best article",
"id": "kb_knowledge:33",
"secondary_fields": [
{
"display_value": "Test Admin",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xx"
},
{
"display_value": "25",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "25"
},
{
"display_value": "2020-01-13 09:44:54",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2020-01-13 17:44:54"
},
{
"display_value": "2020-01-13 09:42:51",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2020-01-13 17:42:51"
}
],
},
{
"external": false,
"link": "--",
"direct": false,
"display_field": "How to connect the iPod to Wi-Fi",
"id": "kb_knowledge:11",
"secondary_fields": [
{
"display_value": "John",
"name": "author",
"label": "Author",
"type": "reference",
"value": "xxnnx"
},
{
"display_value": "16",
"name": "sys_view_count",
"label": "View count",
"type": "integer",
"value": "16"
},
{
"display_value": "2019-12-18 08:18:08",
"name": "sys_updated_on",
"label": "Updated",
"type": "glide_date_time",
"value": "2019-12-18 16:18:08"
},
{
"display_value": "2019-10-21 12:27:22",
"name": "sys_created_on",
"label": "Created",
"type": "glide_date_time",
"value": "2019-10-21 19:27:22"
}
],
}
]
arr.sort(function(a, b){
return new Date(b.secondary_fields[3].display_value) - new Date(a.secondary_fields[3].display_value)
})
console.log(arr)

Convert nested JSON to flat JSON - In order to insert them as records in SQL

I have a JSON string in nested pattern like below:
NESTED (Just Example):
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
},
"topping":
[
{ "id": "5001", "type": "None" },
{ "id": "5002", "type": "Glazed" },
{ "id": "5005", "type": "Sugar" },
{ "id": "5007", "type": "Powdered Sugar" },
{ "id": "5006", "type": "Chocolate with Sprinkles" },
{ "id": "5003", "type": "Chocolate" },
{ "id": "5004", "type": "Maple" }
]
}
I want an option or way to convert this nested JSON string to a flat JSON string so that I can insert them like a record in SQL table.
Can you please help on how to structure the JSON from nested to flat?

Nested Loops in angularjs

I have been using nested loops to access data of the json object to display the id and type of topping, however its not working. Here's my code:
var j_obj = {
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [{
"id": "1001",
"type": "Regular"
}, {
"id": "1002",
"type": "Chocolate"
}, {
"id": "1003",
"type": "Blueberry"
}, {
"id": "1004",
"type": "Devil's Food"
}]
},
"topping": [{
"id": "5001",
"type": "None"
}, {
"id": "5002",
"type": "Glazed"
}, {
"id": "5005",
"type": "Sugar"
}, {
"id": "5007",
"type": "Powdered Sugar"
}, {
"id": "5006",
"type": "Chocolate with Sprinkles"
}, {
"id": "5003",
"type": "Chocolate"
}, {
"id": "5004",
"type": "Maple"
}]
}
var Outer_log=[];
debugger
angular.forEach(j_obj, function(an_object){
//Outer_log.push("ID : "+an_object.id+" type : "+an_object.type);
angular.forEach(an_object.topping,function(innerobject){
Outer_log.push("ID : "+innerobject.id+" type : "+innerobject.type);
},Outer_log);
});
console.log(Outer_log);
Could someone please highlight the error in above code, Thanks
Without using nested loop you can iterate using angular.forEach like this
var finalArray=[];
angular.forEach(j_obj[0].topping, function(eachobject){
finalArray.push("ID : "+ eachobject.id+" type : "+ eachobject.type);
});
Angulars forEach is intended to iterate over arrays not object. so if you change your code to something like this
var j_obj = [{ ...}] //object is wrapped inside array.
it will work. Another thing is you don't need a nested loop in this case. You can just do:
angular.forEach(j_obj.topping, function(key, value){ ... });
you are iterating over object where as loop run over array.
hope this helps JSfiddle link
var j_obj = [{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [{
"id": "1001",
"type": "Regular"
}, {
"id": "1002",
"type": "Chocolate"
}, {
"id": "1003",
"type": "Blueberry"
}, {
"id": "1004",
"type": "Devil's Food"
}]
},
"topping": [{
"id": "5001",
"type": "None"
}, {
"id": "5002",
"type": "Glazed"
}, {
"id": "5005",
"type": "Sugar"
}, {
"id": "5007",
"type": "Powdered Sugar"
}, {
"id": "5006",
"type": "Chocolate with Sprinkles"
}, {
"id": "5003",
"type": "Chocolate"
}, {
"id": "5004",
"type": "Maple"
}]
}]
var Outer_log = [];
angular.forEach(j_obj, function(an_object) {
//Outer_log.push("ID : "+an_object.id+" type : "+an_object.type);
angular.forEach(an_object.topping, function(innerobject) {
Outer_log.push("ID : " + innerobject.id + " type : " + innerobject.type);
}, Outer_log);
});
console.log(Outer_log);

Sort nested array by properties [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 8 years ago.
I have this array and I was wondering if I could sort it with the properties comments.count and likes.count separately.
For example I could just have a function to call using likes.count parameter or comments.count parameter.
{
"attribution": null,
"tags": [
],
"type": "",
"location": null,
"comments": {
"count": 2,
"data": [
{
"created_time": "1385670850",
"text": "Holy shit",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
"id": "599372997379438683"
},
{
"created_time": "1385680581",
"text": "",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
},
"id": "599454624038205610"
}
]
},
"likes": {
"count": 6,
"data": [
{
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
"id": "2169761"
},
{
"username": "rashmityagi",
"profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
"id": "393770264"
},
{
"username": "tylerferweda",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
{
"username": "cbolts",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
}
]
}
},
{
"attribution": null,
"tags": [
],
"type": "",
"location": null,
"comments": {
"count": 10,
"data": [
{
"created_time": "1385670850",
"text": "Holy shit",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
"id": "599372997379438683"
},
{
"created_time": "1385680581",
"text": "",
"from": {
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
},
"id": "599454624038205610"
}
]
},
"likes": {
"count": 20,
"data": [
{
"username": "someone",
"profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
"id": "2169761"
},
{
"username": "rashmityagi",
"profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
"id": "393770264"
},
{
"username": "tylerferweda",
"profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
"id": "191120775"
},
{
"username": "cbolts",
"profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
"id": "1523167"
}
]
}
},
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
json_array.sort(function(a, b) {
if(a.comments.count < b.comments.count) {
return -1;
}
if(a.comments.count > b.comments.count) {
return 1;
}
return 0;
});
You can also modify it for likes.count
Underscore.js provides great utility functions for working with arrays and objects. For your case you could use _.sortBy(...) See http://underscorejs.org/#sortBy for more details. For simple object properties it is enough to specify the object property name als last parameter.

How to loop inside a json file with javascript?

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

Categories