使用数组循环连接内容
concatenate content using loop over array
我正在开发一个 Bixby 胶囊,让用户可以访问免费和付费内容 "packs"。每个包都是一个存储在 content/ 目录中的文件。我想遍历这些文件并将它们读入一个名为 entitled_content.
的变量中
我从事实胶囊开始,它使用实用函数搜索名为 content.js 的本地文件。
const CONTENT = []
const literature = require("../content/literature")
const enhanced = require("../content/enhanced")
const roosevelt = require("../content/roosevelt")
const ambition = require("../content/ambition")
const chaucer = require ("../content/chaucer")
//const GET_REMOTE = require('./lib/getRemoteContent.js')
var console = require('console')
console.log(roosevelt)
console.log(ambition)
console.log(chaucer)
const entitlements = ["roosevelt", "ambition", "chaucer"]
var entitled_content = []
entitlements.forEach(function (item) {
entitled_content = entitled_content.concat(item)
console.log(item); })
console.log(entitled_content)
它的作用是这样的:
[ { tags: [ 'roosevelt' ],
text: 'Happiness is not a goal; it is a by-product. --Eleanor Roosevelt',
image: { url: 'images/' } } ]
[ { tags: [ 'ambition' ],
text: 'Ambition is but avarice on stilts, and masked. --Walter Savage Landor' } ]
[ { tags: [ 'literature' ],
text: 'A man was reading The Canterbury Tales one Saturday morning, when his wife asked What have you got there? Replied he, Just my cup and Chaucer.' },
{ tags: [ 'literature' ],
text: 'For years a secret shame destroyed my peace-- I\'d not read Eliot, Auden or MacNiece. But now I think a thought that brings me hope: Neither had Chaucer, Shakespeare, Milton, Pope. Source: Justin Richardson.' } ]
roosevelt
ambition
chaucer
[ 'roosevelt', 'ambition', 'chaucer' ]
我想要它做的是 assemble 这三个文件 roosevelt、ambitio 和 chaucer 到一个数组变量 entitled_content 中,然后由实用程序函数进行搜索。错误的是这一行 entitled_content = entitled_content.concat(item) 没有做我想要它做的事情,即获取名为 "item".[= 的文件的全部内容12=]
因为您将变量名用引号括起来,所以程序将它们作为字符串读取。
从
更改
const entitlements = ["roosevelt", "ambition", "chaucer"]
到
const entitlements = [roosevelt, ambition, chaucer]
我正在开发一个 Bixby 胶囊,让用户可以访问免费和付费内容 "packs"。每个包都是一个存储在 content/ 目录中的文件。我想遍历这些文件并将它们读入一个名为 entitled_content.
的变量中我从事实胶囊开始,它使用实用函数搜索名为 content.js 的本地文件。
const CONTENT = []
const literature = require("../content/literature")
const enhanced = require("../content/enhanced")
const roosevelt = require("../content/roosevelt")
const ambition = require("../content/ambition")
const chaucer = require ("../content/chaucer")
//const GET_REMOTE = require('./lib/getRemoteContent.js')
var console = require('console')
console.log(roosevelt)
console.log(ambition)
console.log(chaucer)
const entitlements = ["roosevelt", "ambition", "chaucer"]
var entitled_content = []
entitlements.forEach(function (item) {
entitled_content = entitled_content.concat(item)
console.log(item); })
console.log(entitled_content)
它的作用是这样的:
[ { tags: [ 'roosevelt' ],
text: 'Happiness is not a goal; it is a by-product. --Eleanor Roosevelt',
image: { url: 'images/' } } ]
[ { tags: [ 'ambition' ],
text: 'Ambition is but avarice on stilts, and masked. --Walter Savage Landor' } ]
[ { tags: [ 'literature' ],
text: 'A man was reading The Canterbury Tales one Saturday morning, when his wife asked What have you got there? Replied he, Just my cup and Chaucer.' },
{ tags: [ 'literature' ],
text: 'For years a secret shame destroyed my peace-- I\'d not read Eliot, Auden or MacNiece. But now I think a thought that brings me hope: Neither had Chaucer, Shakespeare, Milton, Pope. Source: Justin Richardson.' } ]
roosevelt
ambition
chaucer
[ 'roosevelt', 'ambition', 'chaucer' ]
我想要它做的是 assemble 这三个文件 roosevelt、ambitio 和 chaucer 到一个数组变量 entitled_content 中,然后由实用程序函数进行搜索。错误的是这一行 entitled_content = entitled_content.concat(item) 没有做我想要它做的事情,即获取名为 "item".[= 的文件的全部内容12=]
因为您将变量名用引号括起来,所以程序将它们作为字符串读取。
从
更改const entitlements = ["roosevelt", "ambition", "chaucer"]
到
const entitlements = [roosevelt, ambition, chaucer]