I'm retrieving information from a database. The data looks like this (I've simplified it):
var example = [
{'start': 1966, 'end': 1970},
{'start': 1969, 'end': 1971},
{'start': 1972, 'end': 1980},
{'start': 1974, 'end': 1985},
{'start': 1975, 'end': 1979},
{'start': 1986, 'end': 1990},
{'start': 1991, 'end': 1995}
];
What I want to do is sort this dynamically into a new, empty array newArr
. When the sorting is done newArr
should look like
var newArr = [
[
{'start': 1966, 'end': 1970},
{'start': 1972, 'end': 1980},
{'start': 1986, 'end': 1990},
{'start': 1991, 'end': 1995}
],
[
{'start': 1969, 'end': 1971},
{'start': 1974, 'end': 1985}
],
[
{'start': 1975, 'end': 1979}
]];
I'm new with javascript. The reason I've chosen for this combination of arrays and objects is because I'm using JSON data in which the order of objects is important
What I want to do and what I tried
I'm trying to group the objects on the different keys of the main array (newArr[0], newArr[1] etc . I want to iterate through example
and put objects on a certain key. if in the iteration of example the property end
is lower than what at that moment is on newArr, there is an overlap and a new array should be made. Else it should be pushed in to the main array key where where there is no overlap. I've tried doing this with the the following three functions
var newArr = [];
function _overlap(){
// place first object
var addFirst = [example[0]];
newArr.push(addFirst);
// place others, therefore start with i = 1;
for (var i = 1 ; i < example.length ; i++){
_checkOverlap(example[i]);
}
}
_overlap();
function _checkOverlap(input){
// traverse the main array newArr, example[i] is passed as argument input
loopJ:{
for (var j = 0; j < newArr.length; j++){
// compare value of input.start (so example[i]) with last key of inner array
var innerArrayLength = newArr[j].length; // I need this to get the last key: length -1
if (input.start > newArr[j][innerArrayLength-1].end ){
newArr[j].push(input);
console.log(newArr);
break loopJ;
} else {
_createNewArr(input);
break loopJ;
}
}
}
}
function _createNewArr(input){
var toBeAdded = [];
toBeAdded.push(input);
newArr.push(toBeAdded);
}
This code does exactly what I want on the first key newArr0 , but never pushes into the other keys. Should I use a recursion in this case? I've tried so many things, but the amount of browsers crashes due to infinite loops is driving me crazy.
If I understand it correctly, for each entry of example
you must search the first group in newArr
where the entry can be added. If you find that group, add the entry and iterate the next entry. If you don't find that group, just create a new group at the end.
var newArr = [];
outerloop:
for(var entry of example) {
for(var group of newArr)
if(group[group.length-1].end < entry.start) {
group.push(entry);
continue outerloop;
}
newArr.push([entry]);
}
why don't you use a comparator function inside arr.sort
var example = [
{'start': 1966, 'end': 1970},
{'start': 1969, 'end': 1971},
{'start': 1972, 'end': 1980},
{'start': 1974, 'end': 1985},
{'start': 1975, 'end': 1979},
{'start': 1986, 'end': 1990},
{'start': 1991, 'end': 1995}
];
//sorting using comparator function
example.sort(function(a,b){return a.start-b.start}) //will sort on start date
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.