2015-11-08 94 views
-2

我有一個國家陣列和城市+國家陣列。Javascript:把數組放在一起

陣列1(國家)以及它是如何製造:

query({ 
    'ids': ids, 
    'dimensions': 'ga:country', 
    'metrics': 'ga:sessions', 
    'sort': '-ga:sessions', 
    'start-date': moment(now).subtract('days', 730).format('YYYY-MM-DD'), 
    'end-date': moment(now).format('YYYY-MM-DD') 
}) 
.then(function(response) { 

    var countries = []; 
    response.rows.forEach(function(row, i) { 
    countries.push({ value: +row[0]+': '+row[1], color: 'white', country: row[0], text: row[0]+': '+row[1]}); 
//text = country and visits 
    }); 
}); 

陣列2(城市):

query({ 
    'ids': ids, 
    'dimensions': 'ga:city, ga:country', 
    'metrics': 'ga:sessions', 
    'sort': '-ga:sessions', 
    'start-date': moment(now).subtract('days', 730).format('YYYY-MM-DD'), 
    'end-date': moment(now).format('YYYY-MM-DD') 
    }) 
    .then(function(response) { 
    var cities = []; 
    response.rows.forEach(function(row, i) { 
     cities.push({ value: +row[1]+': '+row[0], color: 'white', label: row[0]+': '+row[2]}); 
//label = city and visits 
//value = country and city 
    }); 
    }); 

我想這一點:

COUNTRY1 - 245

  • City1 - 105
  • 城2 - 140

COUNTRY2 - 1180

  • City5 - 530
  • City8 - 650

我不知道從這裏開始。有人能幫我嗎?

回答

0

你需要一個對象中的數組,例如:

{ 
    "country-1": ["city-1", "city-2"], 
    "country-2": ["city-3", "city-4"] 
} 
+0

沒錯。但是我在帖子中描述的那兩個陣列來自Google Analytics,所以我無法更改它們。 –

+0

我很抱歉,但我不明白。你不需要改變它們,你只需要將它們推入你的對象。 – Zak

+0

你的意思是我必須從各個國家制造物品,然後在國家匹配的國家推動這些城市? –