Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var arr = ["Germany / Argentina: 1-0", "Brazil / Netherlands: 0-3", "Netherlands / Argentina: 0-0", "Brazil / Germany: 1-7",
- "Argentina / Belgium: 1-0", "Netherlands / Costa Rica: 0-0", "France / Germany: 0-1", "Brazil / Colombia: 2-1" ]
- mess(arr)
- function mess (input){
- var result = {}
- var arr = []
- var obj = {}
- for (var i = 0; i < input.length; i++) {
- var a = (input[i].trim().split(':')); //console.log(a)
- var key1 = ((a[0].trim().split('/'))[0]).trim(); //console.log(key1)
- var key2 = ((a[0].trim().split('/'))[1]).trim(); //console.log(key2)
- var value1 = Number((((a[1]).trim().split('-'))[0]).trim()); //console.log(value1)
- var value2 = Number((((a[1]).trim().split('-'))[1]).trim()); //console.log(value2)
- obj[key1] = value1
- obj[key2] = value2; //console.log(obj)
- arr.push(obj)
- obj = {}
- }
- // console.log(arr)
- for (var i = 0; i < arr.length; i++) {
- var countryfirst = Object.keys(arr[i])[0];
- var countrysecond = Object.keys(arr[i])[1]//console.log(country)
- if (!result[countryfirst]){
- result[countryfirst]= {goalsScored:0,goalsConceded:0,matchesPlayedWith:[] }
- }
- if (result[countryfirst].matchesPlayedWith.indexOf(countrysecond)==-1)
- {
- result[countryfirst].matchesPlayedWith.push(countrysecond)
- }
- // result[countryfirst].matchesPlayedWith.push(countrysecond)
- result[countryfirst].goalsScored = result[countryfirst].goalsScored + arr[i][Object.keys(arr[i])[0]]
- result[countryfirst].goalsConceded = result[countryfirst].goalsConceded + arr[i][Object.keys(arr[i])[1]]
- if (!result[countrysecond]){
- result[countrysecond]= {goalsScored:0,goalsConceded:0,matchesPlayedWith:[] }
- }
- if (result[countrysecond].matchesPlayedWith.indexOf(countryfirst)){
- result[countrysecond].matchesPlayedWith.push(countryfirst)
- }
- result[countrysecond].goalsScored = result[countrysecond].goalsScored + arr[i][Object.keys(arr[i])[1]]
- result[countrysecond].goalsConceded = result[countrysecond].goalsConceded + arr[i][Object.keys(arr[i])[0]]
- }
- var keysarr = Object.keys(result)
- keysarr.sort(); //console.log(keysarr)
- var res = {}
- for (var i = 0; i < keysarr.length; i++) {
- res[keysarr[i]] = result[keysarr[i]]
- }
- // for (var i = 0; i < Object.keys(res).length; i++) {
- // res[i].matchesPlayedWith.sort()
- // }
- for (var j in res){ res[j].matchesPlayedWith.sort()}
- console.log(JSON.stringify(res))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement