Javascript 数组以 3 为一组
Javascript array grouped in 3's
我正在尝试解析 rss 提要。
但是我想要得到的是:
1) 一个包含 3 个元素的数组。所以我有不同的提要,只有它自己的数据组合在一起 - 所以调用起来很容易。
示例:
myArr = [{title, date, content}, {title,date,content}, ....... ]
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>   submitted by   <a href="https://www.reddit.com/user/Daului
gi51"> /u/Dauluigi51 </a>   to   <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>   <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>
  submitted by   <a href="https://www.reddit.com/user/morgul702"> /u/mor
gul702 </a>   to   <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>
  <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>
+++++++++++++++
如果我很懂你
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}, ....... ]
Promise 允许您从回调外部访问异步函数的结果。我相信这就是您要找的:
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);
});
我正在尝试解析 rss 提要。 但是我想要得到的是:
1) 一个包含 3 个元素的数组。所以我有不同的提要,只有它自己的数据组合在一起 - 所以调用起来很容易。
示例: myArr = [{title, date, content}, {title,date,content}, ....... ]
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>   submitted by   <a href="https://www.reddit.com/user/Daului
gi51"> /u/Dauluigi51 </a>   to   <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>   <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>
  submitted by   <a href="https://www.reddit.com/user/morgul702"> /u/mor
gul702 </a>   to   <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>
  <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>
+++++++++++++++
如果我很懂你
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}, ....... ]
Promise 允许您从回调外部访问异步函数的结果。我相信这就是您要找的:
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);
});