如何在函数中获取值并替换变量

时间:2017-05-04 13:12:11

标签: javascript angularjs node.js firebase firebase-realtime-database

现在我从firebase获取数据

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' } ]
});

我该怎么做?

2 个答案:

答案 0 :(得分: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));




答案 1 :(得分: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
    });
}