2017-05-04 79 views
-1

現在我從火力如何在功能上得到的價值和替換變量

database = firebase.database(); 
var ref = database.ref('urls'); 
ref.on('value', gotData, errData); 

function errData(err){ 
    console.log('Error!'); 
    console.log(err); 
} 

function gotData(data){ 
    result = data.val() 
    const urls = Object.keys(result) 
       .filter(key => result[key].last_res > 5) 
       .map(key => ({url: 's/price/' + result[key].url_site + '/'})) 
    console.log(urls) 
} 

結果網址獲得的數據是

[ { url: '/cream/' }, 
    { url: '/salmon/' }, 
    { url: '/salmon-s/' } ] 

我需要在URL中發送數據替換如下

var sitemap = sm.createSitemap({ 
    hostname: 'xxx.com', 
    urls: [ { url: 'xxx' }, 
    { url: 'xxx' }, 
    { url: 'xxx' } ] 
}); 

我該怎麼做?

回答

0

是否這樣?

const a = [ { url: '/cream/' }, 
 
    { url: '/salmon/' }, 
 
    { url: '/salmon-s/' } ] 
 
    
 
const sm = Object.assign({ hostname: 'xxx.com'}, { urls: a}) 
 

 
console.log(JSON.stringify(sm, null, 2));

0

,您可以撥打createSitemap()功能和gotData()

function gotData(data){ 
    result = data.val() 
    const urls = Object.keys(result) 
       .filter(key => result[key].last_res > 5) 
       .map(key => ({url: 's/price/' + result[key].url_site + '/'})) 
    createSitemap(urls) 
} 

function createSitemap(urls) { 
    var sitemap = sm.createSitemap({ 
     hostname: 'xxx.com', 
     urls: urls 
    }); 
} 
通過URL來它