This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 4 years ago.
I am getting an array of "product"s from a resolver getting data from a json endpoint.
ngOnInit() {
this.products = this._route.snapshot.data.products;
console.log('products: ', this.products);
}
where one of the objects in this array is in the format
{
"id": 3645,
"date": "2018-07-05T13:13:37",
"date_gmt": "2018-07-05T13:13:37",
"guid": {
"rendered": ""
},
"modified": "2018-07-05T13:13:37",
"modified_gmt": "2018-07-05T13:13:37",
"slug": "vpwin",
"status": "publish",
"type": "matrix",
"link": "",
"title": {
"rendered": "VPWIN"
},
"content": {
"rendered": "",
"protected": false
},
"featured_media": 0,
"parent": 0,
"template": "",
"better_featured_image": null,
"acf": {
"domain": "SMB",
"ds_rating": "3",
"dt_rating": ""
},
...
},
What I want to do is sort this array by the field title.rendered
In olden times, in AngularJS, I would simply use an orderBy pipe in the template set to this field. Apparently, this is removed in Angular and from doing research it seems the preferred method is to sort the data itself, such as in ngOnInit.
But I can't figure out how to sort products by producs.title.rendered.
You can simply use Arrays.sort()
array.sort((a,b) => a.title.rendered.localeCompare(b.title.rendered));
Working Example :
var array = [{"id":3645,"date":"2018-07-05T13:13:37","date_gmt":"2018-07-05T13:13:37","guid":{"rendered":""},"modified":"2018-07-05T13:13:37","modified_gmt":"2018-07-05T13:13:37","slug":"vpwin","status":"publish","type":"matrix","link":"","title":{"rendered":"VPWIN"},"content":{"rendered":"","protected":false},"featured_media":0,"parent":0,"template":"","better_featured_image":null,"acf":{"domain":"SMB","ds_rating":"3","dt_rating":""},},{"id":3645,"date":"2018-07-05T13:13:37","date_gmt":"2018-07-05T13:13:37","guid":{"rendered":""},"modified":"2018-07-05T13:13:37","modified_gmt":"2018-07-05T13:13:37","slug":"vpwin","status":"publish","type":"matrix","link":"","title":{"rendered":"adfPWIN"},"content":{"rendered":"","protected":false},"featured_media":0,"parent":0,"template":"","better_featured_image":null,"acf":{"domain":"SMB","ds_rating":"3","dt_rating":""}},{"id":3645,"date":"2018-07-05T13:13:37","date_gmt":"2018-07-05T13:13:37","guid":{"rendered":""},"modified":"2018-07-05T13:13:37","modified_gmt":"2018-07-05T13:13:37","slug":"vpwin","status":"publish","type":"matrix","link":"","title":{"rendered":"bbfPWIN"},"content":{"rendered":"","protected":false},"featured_media":0,"parent":0,"template":"","better_featured_image":null,"acf":{"domain":"SMB","ds_rating":"3","dt_rating":""}}];
array.sort((a,b) => a.title.rendered.localeCompare(b.title.rendered));
console.log(array);
Try this
products.sort(function (a, b) {
return a.title.rendered - b.title.rendered;
});
OR
You can import lodash/underscore library, it has many build functions available for manipulating, filtering, sorting the array and all.
Using underscore: (below one is just an example)
import * as _ from 'underscore';
let sortedArray = _.sortBy(array, 'title');
Not tested but should work
products.sort((a,b)=>a.title.rendered > b.title.rendered)
This question already has answers here:
Searching for items in a JSON array Using Node (preferably without iteration)
(6 answers)
Closed 5 years ago.
I have the following function that receives a JSON array templates and returns a JSON object.
The code executes the console.log. However, it always returns the null at the end of the function. Any ideas?
function getTemplate(templates, myId) {
Object.keys(templates).some(obj => {
if (templates[obj].id === myId) {
console.log('HELLO');
return templates[obj];
}
});
return null;
}
template array
[
{
"id": 80,
"name": "template 1"
},
{
"id": 81,
"name": "template 2"
}
]
However, it always returns null.
Here is working example,
use .find instead of .some. We always expect to get one object.
function getTemplate(templates, myId) {
return templates.find(template => template.id === myId);
}
var array =
[
{
"id": 80,
"name": "template 1"
},
{
"id": 81,
"name": "template 2"
}
]
console.log(getTemplate(array, 80));
This question already has answers here:
Converting a JS object to an array using jQuery
(18 answers)
Closed 5 years ago.
my server upon request, serves JSON from a node mysql query, but only the names row. Example:
{
"Name": "Charles"
}, etc.
How can I, get the value of Name and put it into an array? say I have this,
[
{
"Name": "Charles"
},
{
"Name": "Alex"
}
]
how can I get, Charles and Alex, into an array?
Like:
Names = ["Charles", "Alex]?
You can make use of the map function. The map method creates a new array with the results of calling a provided function on every element in this array. In your case, you need to select only the Name.
var arr = [
{
"Name": "Charles"
},
{
"Name": "Alex"
}];
var names = arr.map(x=>x.Name)
console.log(names);
This question already has answers here:
draw google graph from json [closed]
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Parse JSON in JavaScript? [duplicate]
(16 answers)
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 8 years ago.
hi i have generated following json , can i iterate through this
i have validated this josn using some sites
following is just example there can be more section like MATHS, LOGICAL REASONING ,ENGLISH
they can also have their individual types
{ "MATHS": [
{
"Section": "MATHS",
"Type": "INCORRECT",
"Count": "5"
},
{
"Section": "MATHS",
"Type": "NOT SOLVED",
"Count": "20"
}
],
"LOGICAL REASONING": [
{
"Section": "LOGICAL REASONING",
"Type": "CORRECT",
"Count": "1"
},
{
"Section": "LOGICAL REASONING",
"Type": "INCORRECT",
"Count": "4"
},
{
"Section": "LOGICAL REASONING",
"Type": "NOT SOLVED",
"Count": "20"
}
]
}
i have searched on may question on stack overflow but none of them can help me
Your JSON has 2 top-level elements. So, you can't logically iterate across the entire document (without flattening it).
But you can iteration across the "MATHS" and "LOGICAL REASONING" elements.
For example, using underscore:
_(data.MATHS).each(function(item) {
var section = item.SECTION;
var type = item.TYPE;
var count = item.COUNT;
// Now do something with them
});
Note the different access method to the 'LOGICAL REASONING' element, because of the space.
_(data['LOGICAL REASONING').each(function(item) {
var section = item.SECTION;
var type = item.TYPE;
var count = item.COUNT;
// Now do something with them
});
If you wanted all the data together, one way of doing it would be:
var flattened = _.union(data.MATHS, data['LOGICAL REASONING']);
_(flattened).each(function(item) {
// Process...
});
Check out the underscore docs for some more information about the possibilities. Many ways to skin a cat.
http://underscorejs.org/
Cheers.
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I am creating a list by fetching values from a JSON file. It is a nested JSON and the list items are- "Thriller","Fiction" which are basically keys for the next level.
on click of the item I'm passing its name(i.e. Thriller/fiction) to another function...
var val = thriller.
Now I need to fetch the value(i.e. "book" & bookname) corresponding to the passed key in this new function. I'm not able to do so using dot operator-
data.library.val not working..
If anybody has worked on something similar..please help..
JSON:
{ "library": [
{
"Thriller": [
{ "book": "ABC" },
{ "book": "DEF" }
]
},
{
"Fiction": [
{ "book": "GHI" },
{ "book": "JKL" }
]
},] }
Code snippet:
$.getJSON('resources/abc.json', function(data){
var i = data.library;
$("#menuList1").css('display','block');
$(i).each(function(key, value){
$.each(value, function(key, value){
console.log(key);
$("#menuList1").append(''+key+'');
});
}); });
Use data.library[key]
MDN Documentation
Your Json is not valid, this },] specially. A good version :
{
"library": [{
"Thriller": [{
"book": "ABC"
}, {
"book": "DEF"
}]
}, {
"Fiction": [{
"book": "GHI"
}, {
"book": "JKL"
}]
}]
}
you can refer the website http://jsonlint.com for validating your json.