Sorting array from Firebase fails - javascript

I'm trying to get an array of users sorted by the number of their likes. I am getting the user from a Firebase database, in case this changes things.
User example:
{
"likes": [
{
"createDate": {
"date": 22,
"day": 3,
"hours": 10,
"minutes": 33,
"month": 7,
"seconds": 25,
"time": 1534934005660,
"timezoneOffset": 0,
"year": 118
},
"creatorID": "1OA4U39rsPPXZblWG6nLHObB7gz1",
"firebaseKey": "-LKWBYQFbaO1Fs8_Rllc",
"workID": "-LK6-jxPZ0tCNW6c-GTI",
"id": "0"
},
{
"createDate": {
"date": 22,
"day": 3,
"hours": 13,
"minutes": 45,
"month": 7,
"seconds": 50,
"time": 1534938350990,
"timezoneOffset": -120,
"year": 118
},
"creatorID": "8Z7dd5L01FW0EbV0Wvu1KaXfmOx1",
"firebaseKey": "-LKWS7ZsUu2Khn0j7vLk",
"workID": "-LK5xZaui7IIcJcT22Rp",
"id": "1"
}
],
}
I was trying to use the sort method like this, but no luck.
var solversByLikes = solvers.sort((a, b) => a.likes.length - b.likes.length);
I am testing it by then console logging the solversByLikes array. This is the result:
0 LikesComponent.js:46
0 LikesComponent.js:46
0 LikesComponent.js:46
0 LikesComponent.js:46
2 LikesComponent.js:46
2 LikesComponent.js:46
But if I switch the a, and b, so that the sorting should be reversed, nothing changes.
var solversByLikes = solvers.sort((a, b) => b.likes.length - a.likes.length);
I tried replicating the issue like this:
allUsers = [{likes: [1, 2, 3, 4, 5, 6, 7, 8]}, {likes: [1, 2,]}, {likes: [1, 2, 3, 4, 5, 6,]}, {likes: [1, 2, 3, 4, 5,]}, {likes: [1, 2, 3, 4,]}, {likes: [1, 2, 3]}]
solversByLikes = allUsers.sort((a, b) => b.likes.length - a.likes.length);
solversByLikes.forEach(user => console.log(user.likes.length));
But this works as intended...
Any idea how to make it sort?
I'm not getting any error or anything.

Related

why am i getting <1 empty item> when using mapping in JavaScript

im using JSON to return an array.
Json:
const data = [{
"week": 1,
"lost": 10,
"recovery_timespan": [{
"week": 2,
"count": 1
}, {
"week": 3,
"count": 0
}],
"netLost": 10,
"netReturned": 20
}, {
"week": 2,
"lost": 7,
"recovery_timespan": [{
"week": 3,
"count": 1
}, {
"week": 4,
"count": 3
}],
"netLost": 30,
"netReturned": 200
}, {
"week": 3,
"lost": 8,
"recovery_timespan": [{
"week": 4,
"count": 1
}],
"netLost": 50,
"netReturned": 40
}];
i need to get the data into a array with lost,counts of recovery_timespan,netLost,netReturned.
Expected Output:
[ [ 10, 1, 0, 10, 20 ],
[ 7, 1, 3, 30, 200 ],
[ 8, 1, 50, 40 ] ]
My approach:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count),
,netLost,netReturned
]);
console.log(result);
and this return array with <1 empty item>:
[ [ 10, 1, 0, <1 empty item>, 10, 20 ],
[ 7, 1, 3, <1 empty item>, 30, 200 ],
[ 8, 1, <1 empty item>, 50, 40 ] ]
Wha is the issue here?
Why am i getting <1 empty item>
You have an extra comma:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count),
here ---> ,netLost,netReturned
]);
Just remove it.
You have an additional , after the nested map:
const result = data.map(({lost, recovery_timespan,netLost,netReturned}) => [
lost,
...recovery_timespan.map(({count}) => count), // <--
,netLost,netReturned
//^--
]);
That creates a hole in the array. That's why you are seeing <1 empty item> in the output
console.log([1,,2])
const res = data.map((el) => [
el.lost,
...el.recovery_timespan.map((timespan) => timespan.count),
/* extra comma here --> */, el.netLost,
el.netReturned
])
[ [ 10, 1, 0, 10, 20 ], [ 7, 1, 3, 30, 200 ], [ 8, 1, 50, 40 ] ]
Not completly sure, but maybe try this.
...recovery_timespan.map(({count}) => count.count)

Push data to array without using if statement

I have following output. it gives my API.
{
"_id": {
"year": 2018,
"month": 6,
"day": 11,
"hour": 12,
"interval": 45,
"method": "200"
},
"count": 1
},
{
"_id": {
"year": 2016,
"month": 11,
"day": 11,
"hour": 16,
"interval": 50,
"method": "404"
},
"count": 5
},
{
"_id": {
"year": 2016,
"month": 11,
"day": 11,
"hour": 17,
"interval": 10,
"method": "200"
},
"count": 47
}}
I want to Push them to arrays according to method. As an example
twoHundArray=[
{ "x":2018,6,11,12,45,
"y" :1},
{"x": 2016,11,11,17,10 ,
"y" :47}]
fourhundrArry=[{ "x":2018,11,11,16,50,
"y" :5}]
without using if/else statement how to push them to different arrays. In here I don't know all the names of methods.so cannot use if statement for "method".that is the problem here.
The original object is invalid. You can't have elements in an object without specifying the keys. I've assumed that it is an array.
Secondly, there is no way of pushing elements to different arrays without knowing their names. So the judgement of pushing the elements to different variables will have to be based on if/else conditions. Additionally, creation of those variables will vary based on the groups, as method could have been any value.
If you agree to group the objects based on the values method have, here is a way to do this:
const data = [{"_id":{"year":2018,"month":6,"day":11,"hour":12,"interval":45,"method":"200"},"count":1},{"_id":{"year":2016,"month":11,"day":11,"hour":16,"interval":50,"method":"404"},"count":5},{"_id":{"year":2016,"month":11,"day":11,"hour":17,"interval":10,"method":"200"},"count":47}];
const res = {};
data.forEach(item => {
const { method, ...obj } = item['_id'];
res[method] = res[method] || [];
res[method].push({
x: Object.values(obj),
y: item.count
});
});
console.log(res);
It creates an object, whose keys are method. The values in the object are the arrays, which contain the items grouped by method.
You can use Array.reduce and create a map based on method. Try the following:
var data = [{
"_id": {
"year": 2018,
"month": 6,
"day": 11,
"hour": 12,
"interval": 45,
"method": "200"
},
"count": 1
},
{
"_id": {
"year": 2016,
"month": 11,
"day": 11,
"hour": 16,
"interval": 50,
"method": "404"
},
"count": 5
},
{
"_id": {
"year": 2016,
"month": 11,
"day": 11,
"hour": 17,
"interval": 10,
"method": "200"
},
"count": 47
}];
var method = data.reduce((a,o)=>{
if(!a[o._id.method]){
a[o._id.method] = [];
};
var { method, ...ob } = o._id;
a[o._id.method].push({
"x": Object.values(ob).join(","),
"y" : o.count
});
return a;
}, {});
console.log(method);
You can create an object with status:values key/pair using Array.reduce and post that using Object destructuring and default assignment, create independent variables.
const arr = [{"_id":{"year":2018,"month":6,"day":11,"hour":12,"interval":45,"method":"200"},"count":1},{"_id":{"year":2016,"month":11,"day":11,"hour":16,"interval":50,"method":"404"},"count":5},{"_id":{"year":2016,"month":11,"day":11,"hour":17,"interval":10,"method":"200"},"count":47}];
let obj = arr.reduce((a,c) => {
a[c._id.method] = a[c._id.method] || [];
a[c._id.method].push({"x" : Object.values(c._id).join(), "y" : c.count});
return a;
},{});
/* You can add an entry here for every status type, it will pick the
** value from object and if not present will be defaulted to an empty array */
const {200 : twoHundArray=[], 404 : fourHundArray=[], 300 : threeHundArray=[]} = obj;
console.log(twoHundArray);
console.log(fourHundArray);
console.log(threeHundArray);
#Palani, I'll suggest you to use an object to gather all the required information.
Please have a look at the below code and let me know any suggestions/modifications if you need.
var timeDataArr = [
{
"_id": {
"year": 2018,
"month": 6,
"day": 11,
"hour": 12,
"interval": 45,
"method": "200"
},
"count": 1
},
{
"_id": {
"year": 2016,
"month": 11,
"day": 11,
"hour": 16,
"interval": 50,
"method": "404"
},
"count": 5
},
{
"_id": {
"year": 2016,
"month": 11,
"day": 11,
"hour": 17,
"interval": 10,
"method": "200"
},
"count": 47
}
]
// An object that maps 'method' to its related data array
var newTimeData = {}
for(var timeData of timeDataArr) {
var obj = timeData["_id"];
var arr = [obj["year"], obj["month"], obj["day"], obj["hour"], obj["interval"]];
var newObj = {
"x": arr.join(", "),
"y": timeData["count"],
}
if(newTimeData[obj["method"] + "Array"]) { // method found
newTimeData[obj["method"] + "Array"].push(newObj)
} else { // method not found
newTimeData[obj["method"] + "Array"] = [newObj]
}
}
// PRETTY PRINTING OBJECT
console.log(JSON.stringify(newTimeData, undefined, 4))
/*...
{
"200Array": [
{
"x": "2018, 6, 11, 12, 45",
"y": 1
},
{
"x": "2016, 11, 11, 17, 10",
"y": 47
}
],
"404Array": [
{
"x": "2016, 11, 11, 16, 50",
"y": 5
}
]
}
...*/
// PRETTY PRINTING ARRAY POINTED BY '200Array' key
console.log(JSON.stringify(newTimeData["200Array"], undefined, 4))
/*...
[
{
"x": "2018, 6, 11, 12, 45",
"y": 1
},
{
"x": "2016, 11, 11, 17, 10",
"y": 47
}
]
...*/
// PRETTY PRINTING ARRAY POINTED BY '404Array' key
console.log(JSON.stringify(newTimeData["404Array"], undefined, 4))
/*...
[
{
"x": "2016, 11, 11, 16, 50",
"y": 5
}
]
...*/
Output ยป
H:\RishikeshAgrawani\Projects\Sof\FilterArrays>node FilterArrays.js
{
"200Array": [
{
"x": "2018, 6, 11, 12, 45",
"y": 1
},
{
"x": "2016, 11, 11, 17, 10",
"y": 47
}
],
"404Array": [
{
"x": "2016, 11, 11, 16, 50",
"y": 5
}
]
}
[
{
"x": "2018, 6, 11, 12, 45",
"y": 1
},
{
"x": "2016, 11, 11, 17, 10",
"y": 47
}
]
[
{
"x": "2016, 11, 11, 16, 50",
"y": 5
}
]

JS Loop through array based on key values and add values

I have the following object inside an array:-
[
{"score": 5, "question": 0, "weight": 2},
{"score": 4, "question": 1, "weight": 2},
{"score": 3, "question": 0, "weight": 4},
{"score": 4, "question": 1, "weight": 4},
{"score": 2, "question": 2, "weight": 4},
{"score": 8, "question": 0, "weight": 2}
]
I am trying to loop through the array so I have the following output, so I am able to run some math against the results:-
[
[
{"score": 5, "question": 0, "weight": 2},
{"score": 4, "question": 1, "weight": 2}
],
[
{"score": 3, "question": 0, "weight": 4},
{"score": 4, "question": 1, "weight": 4},
{"score": 2, "question": 2, "weight": 4}
],
[
{"score": 8, "question": 0, "weight": 2}
]
];
Is there a dynamic way I am able to get array1 to look like array2?
I am using flat JS for this please no jQuery answers.
Thanks in advance.
** Note **
Sometimes each section will have more or less values, this is why I require it to be dynamic.
You can do this with reduce() method you just need to keep track of current index for final array.
const data =[
{score: 5, question: 0, weight: 2},
{score: 4, question: 1, weight: 2},
{score: 3, question: 0, weight: 4},
{score: 4, question: 1, weight: 4},
{score: 2, question: 2, weight: 4},
{score: 8, question: 0, weight: 2}
]
const result = data.reduce(function(r, e, i) {
if(i == 0) r = {values: [], counter: 0}
if(e.question == 0 && i != 0) r.counter++
if(!r.values[r.counter]) r.values[r.counter] = [e]
else r.values[r.counter].push(e)
return r;
}, {}).values
console.log(result)
You could check weight and if different, then take a new group.
var data = [{ score: 5, question: 0, weight: 2 }, { score: 4, question: 1, weight: 2 }, { score: 3, question: 0, weight: 4 }, { score: 4, question: 1, weight: 4 }, { score: 2, question: 2, weight: 4 }, { score: 8, question: 0, weight: 2 }],
grouped = data.reduce(function (r, o, i, a) {
if ((a[i - 1] || {}).weight !== o.weight) {
r.push([]);
}
r[r.length - 1].push(o);
return r;
}, []);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use Array.reduce() to convert the 1st to the 2nd array. Whenever the current object weight doesn't match lastWeight, add another subarray. Always push the current item to the last subarray:
const arr = [
{score: 5, question: 0, weight: 2},
{score: 4, question: 1, weight: 2},
{score: 3, question: 0, weight: 4},
{score: 4, question: 1, weight: 4},
{score: 2, question: 2, weight: 4},
{score: 8, question: 0, weight: 2}
];
let lastWeight = null;
const result = arr.reduce((r, o) => {
if(lastWeight === null || o.weight !== lastWeight) {
lastWeight = o.weight;
r.push([]);
}
r[r.length - 1].push(o);
return r;
}, []);
console.log(result);
var oldA = [
{"score": 5, "question": 0, "weight": 2},
{"score": 4, "question": 1, "weight": 2},
{"score": 3, "question": 0, "weight": 4},
{"score": 4, "question": 1, "weight": 4},
{"score": 2, "question": 2, "weight": 4},
{"score": 8, "question": 0, "weight": 2}
];
var newA = [];
var prevW = 0;
var prevA;
for (var i = 0; i < oldA.length; i++) {
if (oldA[i].weight != prevW) {
prevA = [];
newA.push(prevA);
prevW = oldA[i].weight;
}
prevA.push(oldA[i]);
}
console.log(newA);
You can use array#reduce to group your array. Check if the value of question is 0 then push a new array and add the object to it.
var data = [ {"score": 5, "question": 0, "weight": 2}, {"score": 4, "question": 1, "weight": 2}, {"score": 3, "question": 0, "weight": 4}, {"score": 4, "question": 1, "weight": 4}, {"score": 2, "question": 2, "weight": 4}, {"score": 8, "question": 0, "weight": 2}],
result = data.reduce((r,o) => {
if(o.question == 0)
r.push([]);
r[r.length - 1].push(o);
return r;
},[]);
console.log(result);
let scores = [
{"score": 5, "question": 0, "weight": 2},
{"score": 4, "question": 1, "weight": 2},
{"score": 3, "question": 0, "weight": 4},
{"score": 4, "question": 1, "weight": 4},
{"score": 2, "question": 2, "weight": 4},
{"score": 8, "question": 0, "weight": 2}
]
let groupedScores = [], group = [];
scores.forEach((entry) => {
if(entry.question === 0) {
if (group.length) {
groupedScores.push(group);
}
group = [];
}
group.push(entry);
})
groupedScores.push(group)
console.log(groupedScores)
A bit simplified if splitting by just question: 0 :
data = [ { score: 5, question: 0, weight: 2 }, { score: 4, question: 1, weight: 2 },
{ score: 3, question: 0, weight: 4 }, { score: 4, question: 1, weight: 4 },
{ score: 2, question: 2, weight: 4 }, { score: 8, question: 0, weight: 2 } ]
result = data.reduce((r, v) => (v.question ? r[r.length-1].push(v) : r.push([v]), r), [])
console.log( result )

How to sort by an associative array value(Complex array structure)?

Consider the Following:
I am representing data in an array in an HTML table such as:
1) How would I sort the array by b1 or b3?
I have tried:
var o = {
"orgs": {
"655": {
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"depth": 1,
"agents": [207072],
"orgunit_id": "TEAM00655",
"name": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"853": {
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"depth": 2,
"agents": [208162],
"orgunit_id": "TEAM00853",
"name": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"921": {
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"depth": 1,
"agents": [210171, 212842],
"orgunit_id": "TEAM00921",
"name": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"agents": {
"207072": {
"name": "Bongiwe Gwala",
"oid": 655,
"depth": 1,
"aid": "A0207072",
"orgunit_id": "TEAM00655",
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"aname": "A0207072: Bongiwe Gwala",
"oname": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"208162": {
"name": "Nkosikhona MADLALA",
"oid": 853,
"depth": 2,
"aid": "A0208162",
"orgunit_id": "TEAM00853",
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"aname": "A0208162: Nkosikhona MADLALA",
"oname": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"212842": {
"name": "SANELE KHUMALO",
"oid": 921,
"depth": 1,
"aid": "A0212842",
"orgunit_id": "TEAM00921",
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"aname": "A0212842: SANELE KHUMALO",
"oname": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"orglist": [853, 655, 921],
}
function sort_data(data, sortby, asc) {
console.log(data);
if (asc == "asc") {
data.sort(function(a, b) {
a.sortby - b.sortby;
});
} else {
data.sort(function(a, b) {
a.sortby + b.sortby;
});
}
// update_data;
}
var a = sort_data(o, "b1", "asc");
console.log(a);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
This how ever gives me an error( To view the error, open your console )
UPDATE:
I got the sorting to work thanks to #NicolasAlbert.
Now I need to order by the agents aswell. It needs to order by orgs first and then by agents I have tried:
if (asc == "desc") {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
// return d - c;
return b_org - a_org && a_agent - b_agent;
// data.agents[a].data[sortby] - data.agents[b].data[sortby]
});
} else {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return a_org - b_org && a_agent - b_agent;
});
}
This does not work how ever...
Another update
I have modified my code to do:
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
agents = data.orgs[b].agents.sort(function(a, b){
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return b_agent - a_agent
});
return b_org - a_org && agents;
});
But this sorts both orgs and agents at the same time.
LAst update:
I got it to work, sorting both the orgs and the agents, I had to create two sorting functions:
function sort_org(data, sortby, order) {
/*
Sort the orgs
*/
var a_org, b_org;
if (order == "desc") {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
return b_org - a_org;
});
} else {
data.orglist.sort(function(a, b) {
a_org = data.orgs[a].data[sortby];
b_org = data.orgs[b].data[sortby];
return a_org - b_org;
});
}
}
function sort_agent(data, sortby, order) {
/*
Sort the agents
*/
var a_agent, b_agent;
if (order == "desc") {
for (var orgid in data.orglist){
data.orgs[data.orglist[orgid]].agents.sort(function(a, b){
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return b_agent - a_agent
})
}
} else {
for (var orgid in data.orglist){
data.orgs[data.orglist[orgid]].agents.sort(function(a, b){
a_agent = data.agents[a].data[sortby];
b_agent = data.agents[b].data[sortby];
return a_agent - b_agent
})
}
}
}
then I just call the functions consecutively... i.e.
sort_org(o, "b1", "asc");
sort_agent(o, "b1", "asc");
I hope this can help someone...
You can only use .sort method on Array instance, not Object. Your data is store in key/value objects and the order cannot be modified.
If you want order your data, you must introduce Array ([]) in it.
May you want order the orglist array like that:
var o = {
"orgs": {
"655": {
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"depth": 1,
"agents": [207072],
"orgunit_id": "TEAM00655",
"name": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"853": {
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"depth": 2,
"agents": [208162],
"orgunit_id": "TEAM00853",
"name": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"921": {
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"depth": 1,
"agents": [210171, 212842],
"orgunit_id": "TEAM00921",
"name": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"agents": {
"207072": {
"name": "Bongiwe Gwala",
"oid": 655,
"depth": 1,
"aid": "A0207072",
"orgunit_id": "TEAM00655",
"data": {
"cons": 30,
"b3ports": 0,
"b9": 2,
"b1": 25,
"b2": 14,
"b3": 10,
"ports": 0,
"rica": 30
},
"aname": "A0207072: Bongiwe Gwala",
"oname": "TEAM00655: Jabba - Mooi River (Muhammad Jaffar)"
},
"208162": {
"name": "Nkosikhona MADLALA",
"oid": 853,
"depth": 2,
"aid": "A0208162",
"orgunit_id": "TEAM00853",
"data": {
"cons": 356,
"b3ports": 1,
"b9": 8,
"b1": 283,
"b2": 122,
"b3": 77,
"ports": 1,
"rica": 356
},
"aname": "A0208162: Nkosikhona MADLALA",
"oname": "TEAM00853: Jabba - Mooiriver (Bongiwe Gwala)"
},
"212842": {
"name": "SANELE KHUMALO",
"oid": 921,
"depth": 1,
"aid": "A0212842",
"orgunit_id": "TEAM00921",
"data": {
"cons": 22,
"b3ports": 0,
"b9": 2,
"b1": 20,
"b2": 7,
"b3": 5,
"ports": 0,
"rica": 22
},
"aname": "A0212842: SANELE KHUMALO",
"oname": "TEAM00921: Jabba - Nolwazi Zungu"
},
},
"orglist": [853, 655, 921]
}
function sort_data(data, sortby, asc) {
console.log(data);
if (asc == "asc") {
data.orglist.sort(function(a, b) {
data.orgs[a].data[sortby] - data.orgs[b].data[sortby];
});
} else {
data.orglist.sort(function(a, b) {
data.orgs[b].data[sortby] - data.orgs[a].data[sortby];
});
}
// update_data;
}
sort_data(o, "b1", "asc");
console.log(o.orglist);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Correct JSON format

Is this correct json format?
{
"count": {
"hbase": 66,
"java": 63,
"spring": 41,
"Sample": 39,
"minify": 36,
"TS-WS1": 28,
"jprofiler": 26,
"logging": 24,
"SCM": 24,
"csrfguard": 20,
"ldap": 19,
"hadoop": 18,
"jquery": 18,
"android": 17,
"TS-WS2": 17,
"myplace": 16,
"jvm": 16,
"daily": 15,
"oops": 15,
"node.js": 15,
"long": 15,
"css3": 13,
"html5": 13,
"jms": 13,
"ci": 11,
"node": 11,
"backlog": 11,
"jsf": 10,
"groovy": 10,
"outofmemory": 9,
"adf": 9,
"Exception": 9,
"guidelines": 9,
"abc": 9,
"liferay": 8,
"performance": 7,
"Groovy": 7,
"jenkin": 7,
"Hadoop": 6,
"Learning": 6,
"code": 6,
"design": 6,
"CTT4TL": 6,
"": 6,
"eclipse": 5,
"templates": 5,
"apache": 5,
"Node.JS": 5,
"analytics": 5,
"cap": 4,
"CSRFGuard": 4,
"corba": 4,
"pattern": 4,
"EST-WS1": 4,
"web": 4,
"formatter": 4,
"Minify": 4,
"guava": 3,
"oracle": 3,
"security": 3,
"checklists": 3,
"lda": 3,
"ana": 3,
"bi": 3,
"ctt4tl": 3,
"est-ws2": 3,
"exception": 3,
"EST-WS2": 3,
"oop": 3,
"how": 3,
"hibernate": 3,
"LDAP": 2,
"cxf": 2,
"Scala": 2,
"interceptor": 2,
"hudson": 2,
"jenkins": 2,
"sonar": 2,
"viva": 2,
"nfr": 2,
"java7": 2,
"CSS3": 2,
"jpa": 2,
"ppt": 2,
"Hudson": 2,
"template": 2,
"des-ws3": 2,
"Hadoop\/HBase": 1,
"secur": 1,
"csrf": 1,
"DB": 1,
"university": 1,
"abcd": 1,
"jsa": 1,
"LOGGING": 1,
"json": 1,
"rm": 1,
"TS-SCM": 1,
"nak": 1,
"fad": 1,
"presentation": 1,
"est-ws1": 1,
"terna": 1,
"lucene": 1,
"coding": 1,
"log4j": 1,
"JPA": 1,
"theme": 1,
"training": 1,
"secu": 1,
"build": 1,
"css": 1,
"project": 1,
"solr": 1,
"DES-WS": 1,
"intercep": 1,
"test": 1
},
"date": MonMay0612: 19: 48IST2013
}
I receive this JSON on one of my ajax call. And just after receiving it shows "parserror".
My code -
$.ajax({
type: "GET",
url: jsonURL + SEARCH_HISTORY_JSON + EXT_JSON,
dataType: "json",
contentType: "application/json",
async : false,
success: function(data) {
},
error: function(xhr, status, error) {
/* $("#tagCloud").html(getMessage(tagcloud.error));
$("#searchHistory").hide();*/
alert(status);
console.log(status);
}
});
Also please tell me how to access this data. Should I access it like data.data and data.count?
Parse error on line 121:
... }, "date": MonMay0612: 19: 48IS
---------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
http://jsonlint.com/
You need to put your date like this -
"date": "MonMay0612: 19: 48IST2013"
The problem is on the "date" field.
You should treat date fields as strings.
Also, I would recommend using UNIX time for that purpose, because it is easier to parse from javascript.
In the success function, you can access the "count" field like data.count.
http://jsonlint.com/
Parse error on line 121:
... }, "date": MonMay0612: 19: 48IS
---------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
A very easy way to lint your JSON.
Change your date format as following:
"date":"Mon May 06 12:19:48 IST 2013"
& follow the json online editor.i.e. chrome : http://jsoneditoronline.org/
The problem is on the date field.
Please pass date filed value with "MonMay0612: 19: 48IST2013"

Categories

Resources