Skip to content

Instantly share code, notes, and snippets.

@feilongfl
Created February 18, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feilongfl/409dfadc44d1fe6ee81bfb159e8e8d9f to your computer and use it in GitHub Desktop.
Save feilongfl/409dfadc44d1fe6ee81bfb159e8e8d9f to your computer and use it in GitHub Desktop.
function input(date,name1,name2){
this.date = date;
this.name1 = name1;
this.name2 = name2;
this.hash = name1 + name2;
}
inputdata = Array.of(new input(1, 'a', 'b'),new input(2, 'a', 'b'),new input(3, 'a', 'b'),new input(4, 'a', 'c'),new input(5, 'a', 'c'),new input(6, 'a', 'b'),new input(7, 'a', 'b'))
function result(name1,name2,hash,start,end){
this.name = name1;
this.name2 = name2;
this.start = start;
this.end = end;
this.hash = hash;
}
resultTemp = new result(inputdata[0].name1,inputdata[0].name2,inputdata[0].hash,inputdata[0].date,inputdata[0].date);
resultList = Array()
for (i = 1; i < inputdata.length; i++){
if(resultTemp.hash != inputdata[i].hash){
resultList.push(resultTemp);
resultTemp = new result(inputdata[0].name1,inputdata[i].name2,inputdata[i].hash,inputdata[i].date,inputdata[i].date);
}
resultTemp.end = inputdata[i].date;
}
resultList.push(resultTemp);
console.log(resultList);
@feilongfl
Copy link
Author

input:

0: input {date: 1, name1: "a", name2: "b", hash: "ab"}
1: input {date: 2, name1: "a", name2: "b", hash: "ab"}
2: input {date: 3, name1: "a", name2: "b", hash: "ab"}
3: input {date: 4, name1: "a", name2: "c", hash: "ac"}
4: input {date: 5, name1: "a", name2: "c", hash: "ac"}
5: input {date: 6, name1: "a", name2: "b", hash: "ab"}
6: input {date: 7, name1: "a", name2: "b", hash: "ab"}

output:

0: result {name: "a", name2: "b", start: 1, end: 3, hash: "ab"}
1: result {name: "a", name2: "c", start: 4, end: 5, hash: "ac"}
2: result {name: "a", name2: "b", start: 6, end: 7, hash: "ab"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment