2017-09-11 9 views
0

rssフィードを解析しようとしています。 しかし、私が得たいのは、3でグループ化されたJavascript配列

1)配列は3つの要素でグループ化されています。だから私は一緒にグループ化された独自のデータだけで、別のフィードを持っている - それは簡単に呼び出すようになります。

例: myArr = [{タイトル、日付、コンテンツ}、{タイトル、日付、コンテンツ} .......]私はこの配列とからその要素にアクセスするにはどうすればよい

2)この機能の外?

ここでは、私がここで達成しようとしていることを皆さんに見せるためにconsole.logを使用しました。 お世話になりました!私が手

var parser = require('rss-parser'); 


function myFunc(){ 
    parser.parseURL('https://www.reddit.com/.rss', function(err, parsed) { 
     console.log(parsed.feed.title); 
     parsed.feed.entries.forEach(function(entry) { 
     var items = []; 
     items.push(entry.title, entry.pubDate, entry.content.split('src=')[1]); 
     console.log("Title: " + items[0]) 
     console.log("Date: " + items[1]) 
     console.log("Content: " + items[2]); 
     console.log(" +++++++++++++++ ") 

     }); 
    }); 
}; 

myFunc(); 

========

出力:

+++++++++++++++ 
Title: Brave Floridian workers delivering pizza during hurricane Irma. 
Date: 2017-09-10T23:57:01.000Z 
Content: "https://b.thumbs.redditmedia.com/4B5QcikKMP8fU90wk6cpR99LSkppsvBIbo88j 
4YgysY.jpg" alt="Brave Floridian workers delivering pizza during hurricane Irma. 
" title="Brave Floridian workers delivering pizza during hurricane Irma." /> </a 
> </td><td> &#32; submitted by &#32; <a href="https://www.reddit.com/user/Daului 
gi51"> /u/Dauluigi51 </a> &#32; to &#32; <a href="https://www.reddit.com/r/Bikin 
iBottomTwitter/"> r/BikiniBottomTwitter </a> <br/> <span><a href="https://i.redd 
.it/wuu9mews85lz.jpg">[link]</a></span> &#32; <span><a href="https://www.reddit. 
com/r/BikiniBottomTwitter/comments/6zbunz/brave_floridian_workers_delivering_piz 
za_during/">[comments]</a></span> </td></tr></table> 
+++++++++++++++ 
Title: Redditor Provides Specific, Clear Instructions to Aid a User in Helping P 
eople Hit By Hurricane Irma in the U.S. Virgin Islands 
Date: 2017-09-11T04:30:26.000Z 
Content: undefined 
+++++++++++++++ 
Title: A fellow Florida 501st member posted this on his Facebook 
Date: 2017-09-11T00:04:25.000Z 
Content: "https://b.thumbs.redditmedia.com/C1Putt8Dihjv31fxqqpwOD3NHMbEddkMUogsW 
j4DHLA.jpg" alt="A fellow Florida 501st member posted this on his Facebook" titl 
e="A fellow Florida 501st member posted this on his Facebook" /> </a> </td><td> 
&#32; submitted by &#32; <a href="https://www.reddit.com/user/morgul702"> /u/mor 
gul702 </a> &#32; to &#32; <a href="https://www.reddit.com/r/StarWars/"> r/StarW 
ars </a> <br/> <span><a href="https://i.imgur.com/I4XCVOP.jpg">[link]</a></span> 
&#32; <span><a href="https://www.reddit.com/r/StarWars/comments/6zbvyq/a_fellow 
_florida_501st_member_posted_this_on_his/">[comments]</a></span> </td></tr></tab 
le> 
+++++++++++++++ 

答えて

0

約束はyコールバックの外側から非同期関数の結果にアクセスすることができます。これがあなたが探しているものだと思います。

var parser = require('rss-parser'); 

function myFunc() { 
    return new Promise((resolve) => { // Promise to allow async access outside of the function 
     parser.parseURL('https://www.reddit.com/.rss', (err, parsed) => { 
      resolve(parsed.feed.entries.map(entry => ({ title: entry.title, date: entry.pubDate, content: entry.content.split('src=')[1] }))); // Mapping into objects of title, date and entry 
     }); 
    }); 
}; 

myFunc().then((entries) => { 
    console.log(entries[0].title); // Output the title of the first result 
    console.log(entries); 
}); 
+0

ありがとう!私が電話しようとすると、以下の機能が私のres.renderとexpressjsで実行され、Webページに結果が表示されます。 then()のために動作しません。私がそれを取り除くと、それを読むと、それは約束と結びついていません。 res.render内でこの関数を呼び出す方法はありますか?例: –

+0

例: 'app.get( '/'、function(req、res){' res.render( 'index'、{ title: 'My最初の結果のタイトルを出力します。 console.log(エントリ); });} //(最初の結果のタイトルを出力します。 アイテム:[ {id:1、Title1 :(entries [0] .title)}、 {id:2、Title2 :(entries [1] .title)}、 {id:3、Title3 :(entries [2] .title)} ] }); }); ' –

1

私はあなたにも

const items = parsed.feed.entries.map((entry) => ({ 
    title: entry.title, 
    date: entry.pubDate, 
    content: entry.content.split('src=')[1], 
})); 

を理解している場合あなたはつもりですitems = [{title, date, content}, {title,date,content}, ....... ]

のような何かを得ます
関連する問題