简体   繁体   English

Array.find不是函数错误

[英]Array.find is not a function error

I have a value stored in a variable called myStation. 我有一个存储在名为myStation的变量中的值。 Now I'd like to find that value in an array located in another file called station.js. 现在,我想在另一个名为station.js的文件中的数组中找到该值。 When I find a match I would like to grab the stationID. 找到匹配项后,我想获取stationID。 The code I'm using let stationNewName = Stations.find((s) => s.stationName === myStation); 我正在使用的代码let stationNewName = Stations.find((s) => s.stationName === myStation); is causing the error "Error handled: Stations.find is not a function". 导致错误“错误处理:Stations.find不是函数”。 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. 我希望不必加载Lodash库的开销,并认为我应该能够使用基本的JavaScript代码来完成。 Here are excerpts from my code that relate to the error: 以下是与错误相关的代码摘录:

Requires the station.js file 需要station.js文件

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" 下一行是在我的一个处理程序中执行的,其中myStation接收到值“ 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". 下一行产生错误:“已处理错误:Stations.find不是函数”。

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

This is an excerpt from my array in the stations.js file 这是我在stations.js文件中的数组的摘录

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"},
    ],
};

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. 在使用find方法(如果传递的谓词为true时将返回数组的元素)之后,您需要引用成员stationId,因为STATIONS数组中的每个元素都是一个对象。

 '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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM