Sorting Json data based on a field - javascript

I have a Json data that I need to sort before display it. My Json is as below. I need to sort them based on the ColumnLocation.
[{
"Name": "PieChart",
"Id": "1",
"ColumnLocation": "0",
"RowLocation": "0"
}, {
"Name": "Calendar",
"Id": "2",
"ColumnLocation": "1",
"RowLocation": "0"
}, {
"Name": "FavouriteFilter",
"Id": "3",
"ColumnLocation": "2",
"RowLocation": "0"
}, {
"Name": "FilterResults",
"Id": "4",
"ColumnLocation": "0",
"RowLocation": "1"
}, {
"Name": "Watched",
"Id": "5",
"ColumnLocation": "1",
"RowLocation": "1"
}]
i.e the sorted array should have items in following fashion
col : 0, row 0
col : 0, row 1
col : 1, row 0
col : 1, row 1

No need for lodash/underscore. You can use Array.prototype.sort:
Since your values are strings, you must first parse them into numbers and then compare:
let a = [{"Name":"PieChart","Id":"1","ColumnLocation":"0","RowLocation":"0"},{"Name":"Calendar","Id":"2","ColumnLocation":"1","RowLocation":"0"},{"Name":"FavouriteFilter","Id":"3","ColumnLocation":"2","RowLocation":"0"},{"Name":"FilterResults","Id":"4","ColumnLocation":"0","RowLocation":"1"},{"Name":"Watched","Id":"5","ColumnLocation":"1","RowLocation":"1"}]
let sorted = a.sort((a, b) => parseInt(a.ColumnLocation) - parseInt(b.ColumnLocation));
console.log(sorted);

Short and sweet.
let arr = [{"Name":"PieChart","Id":"1","ColumnLocation":"0","RowLocation":"0"},{"Name":"Calendar","Id":"2","ColumnLocation":"1","RowLocation":"0"},{"Name":"FavouriteFilter","Id":"3","ColumnLocation":"2","RowLocation":"0"},{"Name":"FilterResults","Id":"4","ColumnLocation":"0","RowLocation":"1"},{"Name":"Watched","Id":"5","ColumnLocation":"1","RowLocation":"1"}]
arr.sort ( ( a, b ) => { return parseInt ( a.ColumnLocation ) > parseInt ( b.ColumnLocation ) } );
console.log ( arr );
Note that if you don't convert to a number, the sorting can not be what you expect.

Why not use _.sortBy ( http://underscorejs.org/#sortBy ) ?
var mysortedarray = _.sortBy(myarray, 'ColumnLocation');

Related

How to convert 1D array into 2D array with predefined positions in JavaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So, Here is my 1D array,
"seats": [
{
"available": "true",
"column": "0",
"name": "L1",
"row": "0",
},
{
"available": "true",
"column": "1",
"name": "L2",
"row": "0",
},
{
"available": "true",
"column": "0",
"name": "L3",
"row": "1",
},
{
"available": "true",
"column": "1",
"name": "L4",
"row": "1",
},
{
"available": "true",
"column": "0",
"name": "L5",
"row": "2",
},
{
"available": "true",
"column": "1",
"name": "L6",
"row": "2",
},
{
"available": "true",
"column": "0",
"name": "L7",
"row": "3",
},
{
"available": "true",
"column": "1",
"name": "L8",
"row": "3",
},
{
"available": "true",
"column": "0",
"name": "L9",
"row": "4",
},
{
"available": "true",
"column": "1",
"name": "L10",
"row": "4",
},
];
So, above is the sample array, from the above i need to create 2D array and display the name on each.
Expected output:
CR CR CR CR
30-20 10-00
31-21 11-01
In this C - column and R is row for reference !
And so on, how to convert 1d into 2d and position the elements according to the row and columns from the array ?
My code trying's:
seats(list, elementsPerSubArray){
var matrix = [], i, k;
for (i = 0, k = -1; i < list.length; i++) {
if (i % elementsPerSubArray === 0) {
k++;
matrix[k] = [];
}
matrix[k].push(list[i]);
}
for(var i = 0; i < matrix.length; i++) {
var cube = matrix[i];
for(var j = 0; j < cube.length; j++) {
console.log("s");
}
}
}
Its giving a straight line of outputs with missing outputs !kindly help,
You could assign name to the given row/columns.
const
data = [{ available: "true", column: "0", name: "L1", row: "0" }, { available: "true", column: "1", name: "L2", row: "0" }, { available: "true", column: "0", name: "L3", row: "1" }, { available: "true", column: "1", name: "L4", row: "1" }, { available: "true", column: "0", name: "L5", row: "2" }, { available: "true", column: "1", name: "L6", row: "2" }, { available: "true", column: "0", name: "L7", row: "3" }, { available: "true", column: "1", name: "L8", row: "3" }, { available: "true", column: "0", name: "L9", row: "4" }, { available: "true", column: "1", name: "L10", row: "4" }],
result = data.reduce((r, { column, row, name }) => {
if (!r[row]) r[row] = [];
r[row][column] = name;
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I used your seats as a basic variable array here.
In column and row attributes you already have the basic keys for a 2d-array, just need to run through it and put every column or every row together.
I did not make any tests other than check if the sub array is undefined
Now you'd have to loop through both dimensions again and print those names
var seats = []; // your content
let newSeats = [];
// rows as first dimension
for (let i = 0; i < seats.length; i++) {
let row = seats[i].row;
if (newSeats[row] === undefined) newSeats[row] = [];
newSeats[row].push(seats[i]);
}
let newSeats = [];
// columns as first dimension
for (let i = 0; i < seats.length; i++) {
let column = seats[i].column;
if (newSeats[column] === undefined) newSeats[column] = [];
newSeats[column].push(seats[i]);
}

Sorting an array of JavaScript objects according to date

i have that file json:
var Point = [{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-08",
"position": [36.8479648, 10.2793332]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-07",
"position":[ 36.8791039, 10.2656209]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-09",
"position": [36.9922751, 10.1255164]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-10",
"position": [36.9009882, 10.3009531]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-04",
"position": [37.2732415, 9.8713665]}];
How can i sort the objects by the LastUpdate(it's a date) property in ascending and descending order using only JavaScript?
Try like this
Point.sort(function(a, b) {
return (new Date(b.LastUpdate)) - (new Date(a.LastUpdate))
})
DEMO
As your date format is YYYY-MM-DD ( year, followed by month, followed by date ), you can simply compare them as strings using String's localeCompare() method.
Then, you just need to add this to your custom sort function as described below:
var Points = [{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-08",
"position": [36.8479648, 10.2793332]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-07",
"position":[ 36.8791039, 10.2656209]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-09",
"position": [36.9922751, 10.1255164]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-10",
"position": [36.9009882, 10.3009531]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-04",
"position": [37.2732415, 9.8713665]}];
Points.sort(function(a, b){
return a.LastUpdate.localeCompare( b.LastUpdate );
});
console.log( Points );
As string are in ISO format, they can be sorted by direct string comparison, so no need to convert them in different type of object.
As pointed by Nina in comments, chrome uses different sorting algorithm for arrays larger than 10, so after all the benchmark test, the best method to sort will be
1st in performance (Avg. ms per task 0.011690500000258907)
Point.sort(function(a,b) {
return (a.LastUpdate > b.LastUpdate) ? 1 : ((b.LastUpdate > a.LastUpdate) ?-1:0);
});
Than
2nd in performance (Avg. ms per task 0.029657999999879395)
Point.sort(function(a, b) {
return a.LastUpdate.localeCompare( b.LastUpdate );
})
3rd in performance (Avg. ms per task 0.03225850000019418)
Point.sort(function(a, b) {
return (new Date(b.LastUpdate)) - (new Date(a.LastUpdate))
})
You can use the sort function with a compare function.
Point.sort(function(a, b) {
var aSplit = a.LastUpdate.split("-"),
aDate = new Date(aSplit[2], aSplit[1], aSplit[0]),
bSplit = b.LastUpdate.split("-"),
bDate = new Date(bSplit [2], bSplit [1], bSplit [0]);
return aDate - bDate;
});
Simple solution using Array.sort(with ES6 "arrow" function) and Date.parse function:
// ascending order
Point.sort((a, b) => Date.parse(a.LastUpdate) - Date.parse(b.LastUpdate));
ascending and descending :
ascPoint = Point.sort((a,b) => Date.parse(a.LastUpdate) -Date.parse(b.LastUpdate));
descPoint = ascPoint.reverse();

Merging two json array object based on union and intersection

I am trying to merge two json array with objects as element. You may refer to this plunkr file for both json. I have succesfully retrieve the expected final outcome array id, but I do not know how to form back the expected json as below. I am using underscore js for this purpose.
Note: If object exist in newJson and not in currentJson, after merge, it will be inactive state by default.
I am not sure whether I am using the correct approach. This is what I have try:
var newJsonID = _.pluck(newJson, 'id');
var currentJsonID = _.pluck(currentJson, 'id');
var union = _.union(newJsonID, currentJsonID);
var intersection = _.intersection(currentJsonID, newJsonID);
var final = _.difference(union, _.difference( currentJsonID, intersection);
Expected Final Outcome:
[
{
"id": "12",
"property1Name": "1"
"status": "inactive"
},
{
"id": "11",
"property1Name": "1"
"status": "inactive"
},
{
"id": "10",
"property1Name": "1"
"status": "inactive"
},
{
"id": "9",
"property1Name": "1"
"status": "active"
}
]
A solution in plain Javascript with two loops and a hash table for lookup.
function update(newArray, currentArray) {
var hash = Object.create(null);
currentArray.forEach(function (a) {
hash[a.id] = a.status;
});
newArray.forEach(function (a) {
a.status = hash[a.id] || 'inactive';
});
}
var newJson = [{ "id": "12", "property1Name": "1" }, { "id": "11", "property1Name": "1" }, { "id": "10", "property1Name": "1" }, { "id": "9", "property1Name": "1" }],
currentJson = [{ "id": "10", "property1Name": "1", "status": "inactive" }, { "id": "9", "property1Name": "1", "status": "active" }, { "id": "8", "property1Name": "1", "status": "active" }, { "id": "7", "property1Name": "1", "status": "inactive" }];
update(newJson, currentJson);
document.write('<pre>' + JSON.stringify(newJson, 0, 4) + '</pre>');

Angular Firebase retrieving collected data from anonymous users

I have a survey system using Angular and Firebase which stores the results of users answers inside of an object specific to each user. This works well for storing data, but I've realized that it may be difficult to pull the data back out due to each object having a unique name.
I'd like to loop over each object and pull the all of the values together. So for all 50 entries find the total of comprehension.icons.damage[1]
How can I construct a loop that goes over objects with unique names like the objects below?
Here is my json structure
"usersanonymous:-JgTyGt6An3WWyLvnnuu" : {
"comprehension" : {
"-JgTzC0r_H58n7y8Al_-" : {
"date" : 1422154060632,
"icons" : [ {
"damage" : [ null, "0", "3", "3" ],
"ocular" : [ null, "2", "3", "1" ],
"physical therapy" : [ null, "0", "4", "4" ],
"skin" : [ null, "4", "0", "1" ]
} ]
}
}
},
"usersanonymous:-JgU-ryIpI-HR7D4VDkp" : {
"comprehension" : {
"-JgU0MwBwisNbjvRFGOT" : {
"date" : 1422154629142,
"icons" : [ {
"damage" : [ null, "0", "3", "4" ],
"ocular" : [ null, "1", "4", "3" ],
"physical therapy" : [ null, "2", "4", "3" ],
"skin" : [ null, "4", "1", "3" ]
} ]
}
}
}
Given your input data, I would create a function to extract just the data you're interested in. I've written this in raw javascript - if you're using jQuery you may have fun using $.map rather than for (x in y).
var data = {
"usersanonymous:-JgTyGt6An3WWyLvnnuu": {
"comprehension": {
"-JgTzC0r_H58n7y8Al_-": {
"date": 1422154060632,
"icons": [{
"damage": [null, "0", "3", "3"],
"ocular": [null, "2", "3", "1"],
"physical therapy": [null, "0", "4", "4"],
"skin": [null, "4", "0", "1"]
}]
}
}
},
"usersanonymous:-JgU-ryIpI-HR7D4VDkp": {
"comprehension": {
"-JgU0MwBwisNbjvRFGOT": {
"date": 1422154629142,
"icons": [{
"damage": [null, "0", "3", "4"],
"ocular": [null, "1", "4", "3"],
"physical therapy": [null, "2", "4", "3"],
"skin": [null, "4", "1", "3"]
}]
}
}
}
};
function extractComprehension(rawData) {
var result = [];
for (var usersanonymous in rawData) {
usersanonymous = rawData[usersanonymous];
if (usersanonymous.comprehension) {
for (var token in usersanonymous.comprehension) {
token = usersanonymous.comprehension[token];
if (token.icons) {
result.push(token.icons[0]);
}
}
}
}
return result;
}
function sumOf(objectList, property, index) {
var result = 0;
for (var o in objectList) {
var numbers = (objectList[o][property] || []);
if (numbers.length >= index) {
result += parseInt(numbers[index], 10);
}
}
return result;
}
Using this mini api you can get the sum of the properties you're interested in:
// Get the data array.
var comprehension = extractComprehension(data);
// Sum some property.
console.log(sumOf(comprehension, 'damage', 3));

Javascript sort object string with numbers

I have the following JSON object in javascript:
var stuff = [{
"id": "20",
"serial": "0/0/19:46,0/0/149:63"
}, {
"id": "8",
"serial": "0/0/151:215,0/0/151:233"
}, {
"id": "54",
"serial": "0/0/151:26,0/0/151:37"
}, {
"id": "22",
"serial": "0/0/155:29,0/0/155:36"
}, {
"id": "4",
"serial": "0/0/151:48,0/0/151:152"
}];
I would like to know how to sort the object by the "serial" field, leaving it like this (taking into account the value of the integers in the serial string):
var stuff = [{
"id": "20",
"serial": "0/0/19:46,0/0/149:63"
}, {
"id": "54",
"serial": "0/0/151:26,0/0/151:37"
}, {
"id": "4",
"serial": "0/0/151:48,0/0/151:152"
}, {
"id": "8",
"serial": "0/0/151:215,0/0/151:233"
}, {
"id": "22",
"serial": "0/0/155:29,0/0/155:36"
}];
Thanks in advance.
This will do it for you:
var normalizer = /[:\/]/g;
function serialCompare(a, b) {
var alist = a.serial.replace(normalizer, ',').split(','),
blist = b.serial.replace(normalizer, ',').split(','),
i = 0, l = alist.length;
while (alist[i] === blist[i] && i < l) {
i += 1;
};
return (parseInt(alist[i], 10) - parseInt(blist[i], 10));
}
sortedstuff = stuff.sort(serialCompare);
// returns array sorted as you asked
See it in a fiddle.
If you are going to be sorting often, or the list is very long, you should consider creating a "normalized" version of the serial value that gets stored in the object. It could be the array as calculated inside the serialCompare function, or it could be the text number parts padded to the same lengths with leading zeroes.
You have an array of objects, which you want to sort by one of their properties. You could very easily do it like this:
stuff.sort(function(a,b) {return a.serial == b.serial ? 0 : a.serial < b.serial ? -1 : 1;});
Alternatively, you could have a more general function:
function sort(input,prop) {
input.sort(function(a,b) {return a[prop] == b[prop] ? 0 : a[prop] < b[prop] ? -1 : 1;});
}
// call with:
sort(stuff,'serial');

Categories