Search code examples
bixbybixbystudio

concatenate content using loop over array


I have a Bixby capsule in progress that lets users access both free and premium content "packs". Each pack is a file stored in a content/ directory. I want to loop over these files and read them into a variable called entitled_content.

I started from the facts capsule which uses a utility function to search a local file called 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)

What it does is this:

[ { 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' ]

What I want it to do is to assemble these three files roosevelt, ambition and chaucer into a single array variable entitled_content that will then be searched by the utility function. What's wrong is that this line entitled_content = entitled_content.concat(item) isn't doing what I want it to do, which is to get the entire contents of the file named "item".


Solution

  • Because you wrapped your variable names in quotation marks the program reads them as strings.

    Change it from

    const entitlements = ["roosevelt", "ambition", "chaucer"]

    to

    const entitlements = [roosevelt, ambition, chaucer]