最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array.find is not a function error - Stack Overflow

programmeradmin1浏览0评论

I have a value stored in a variable called myStation. Now I'd like to find that value in an array located in another file called station.js. When I find a match I would like to grab the stationID. The code I'm using let stationNewName = Stations.find((s) => s.stationName === myStation); is causing the error "Error handled: Stations.find is not a function". What am I missing?

I was hoping to not have to load the overhead of Lodash library, and thought I should be able to accomplish with basic javascript code. Here are excerpts from my code that relate to the error:

Requires the station.js file

const Stations = require("./stations.js");

Here's an excerpt leading up to the code causing the error. The next line is executed in one of my Handlers where myStation is receiving the value "CBS"

const myStation = handlerInput.requestEnvelope.request.intent.slots.stationName.value;

The next line is producing the error: "Error handled: Stations.find is not a function".

let stationNewName = Stations.find((s) => s.stationName === myStation);

This is an excerpt from my array in the stations.js file

STATIONS: [          
          {stationName: "CBS", stationID: "8532885"},
          {stationName: "NBC", stationID: "8533935"},
          {stationName: "ABC", stationID: "8534048"},
    ],  

Updated Array to include full module

'use strict';

module.exports = {

STATIONS: [          
          {stationName: "CBS", stationID: "8532885"},
          {stationName: "NBC", stationID: "8533935"},
          {stationName: "ABC", stationID: "8534048"},
    ],
};

I have a value stored in a variable called myStation. Now I'd like to find that value in an array located in another file called station.js. When I find a match I would like to grab the stationID. The code I'm using let stationNewName = Stations.find((s) => s.stationName === myStation); is causing the error "Error handled: Stations.find is not a function". What am I missing?

I was hoping to not have to load the overhead of Lodash library, and thought I should be able to accomplish with basic javascript code. Here are excerpts from my code that relate to the error:

Requires the station.js file

const Stations = require("./stations.js");

Here's an excerpt leading up to the code causing the error. The next line is executed in one of my Handlers where myStation is receiving the value "CBS"

const myStation = handlerInput.requestEnvelope.request.intent.slots.stationName.value;

The next line is producing the error: "Error handled: Stations.find is not a function".

let stationNewName = Stations.find((s) => s.stationName === myStation);

This is an excerpt from my array in the stations.js file

STATIONS: [          
          {stationName: "CBS", stationID: "8532885"},
          {stationName: "NBC", stationID: "8533935"},
          {stationName: "ABC", stationID: "8534048"},
    ],  

Updated Array to include full module

'use strict';

module.exports = {

STATIONS: [          
          {stationName: "CBS", stationID: "8532885"},
          {stationName: "NBC", stationID: "8533935"},
          {stationName: "ABC", stationID: "8534048"},
    ],
};
Share Improve this question edited Jun 21, 2018 at 16:30 Hackbrew asked Jun 21, 2018 at 16:08 HackbrewHackbrew 5131 gold badge12 silver badges26 bronze badges 12
  • 3 What is exported from stations.js though? – Pointy Commented Jun 21, 2018 at 16:09
  • 2 Well Stations is not what you think it is most likely. console.log(Stations) – epascarello Commented Jun 21, 2018 at 16:10
  • @epascarello - console.log(Stations) returns the JSON containing the array above. – Hackbrew Commented Jun 21, 2018 at 16:18
  • So than your code would have to reference STATIONS if it is in object being returned. – epascarello Commented Jun 21, 2018 at 16:18
  • @epascarello - Like this: let stationNewName = Stations.find((s) => STATIONS[s.stationName === myStation]);? – Hackbrew Commented Jun 21, 2018 at 16:20
 |  Show 7 more comments

2 Answers 2

Reset to default 9

Your export contains an object with one property that contains an array. So you need to reference that one property of the object to get to the array that you think you are referencing

let stationNewName = Stations.STATIONS.find((s) => s.stationName === myStation);

After using the find method which will return the element of the array if the passed predicate is true you need to reference the member stationId as each element within your STATIONS array is an object.

'use strict';

module.exports = {
  STATIONS: [{
      stationName: "CBS",
      stationID: "8532885"
    },
    {
      stationName: "NBC",
      stationID: "8533935"
    },
    {
      stationName: "ABC",
      stationID: "8534048"
    },
  ],
};

// Import the default export from the stations.js module which is the object containing the STATIONS array.
const Stations = require("./stations.js");

const myStation = 'STATION_NAME';

// Find the first element within STATIONS with the matching stationName
const station = Stations.STATIONS.find((s) => s.stationName === myStation);

// As find will return the found element which is an object you need to reference the stationID member.
const stationId = station.stationID;

发布评论

评论列表(0)

  1. 暂无评论