【问题标题】:Can not read property of array inside forEach push function无法读取 forEach 推送函数中的数组属性
【发布时间】:2020-12-18 23:37:51
【问题描述】:

运行separateInformationToObjects函数时,无法读取forEach push函数内部数组的属性。

在函数separateInformationToObjects 中,数组名称(marketDataUSDPricesObjectPropertiesmarketDataUSDRelativeObjectProperties)在使用时不会像其他名称一样突出显示。

我正在使用 javascript 和 nodeJS

调用函数:

        separateInformationToObjects(marketDataUSD, particleDataAll,particlePriceDataAll,particleVolumeDataAll, particleMarketCapDataAll,marketDataUSDPricesObjectProperties,marketDataUSDRelativeObjectProperties)

数组属性:

let marketDataUSDRelativeObjectProperties = [
    "price_change_percentage_1h_in_currency_relative_percentage",
    "price_change_percentage_24h_in_currency_relative_percentage",
    "price_change_percentage_7d_in_currency_relative_percentage",
    "price_change_percentage_30d_in_currency_relative_percentage",
    "price_change_percentage_200d_in_currency_relative_percentage",
    "price_change_percentage_1y_in_currency_relative_percentage"
]

功能

   function separateInformationToObjects (marketDataUSD, particleDataAll,particlePriceDataAll,particleVolumeDataAll, particleMarketCapDataAll,marketDataUSDPricesObjectProperties,marketDataUSDRelativeObjectProperties) {
        //Push separate information to designated array
        particlePriceDataAll.forEach((array, index) => {
            marketDataUSD.forEach((coin) => {
                array.push({
                    "id": coin.id,
                    "particleInfo": coin.marketDataUSDPricesObjectProperties[index],
                    "relativePercentage": coin.marketDataUSDRelativeObjectProperties[index]
                });
            });
        });
    }

ParticleDatAll 是一个数组数组:

let particleDataPrice1h = [],
    particleDataPrice24h = [],
    particleDataPrice7d = [],
    particleDataPrice30d = [],
    particleDataPrice200d = [],
    particleDataPrice1y = [];

let particlePriceDataAll = [particleDataPrice1h, particleDataPrice24h,particleDataPrice7d ,particleDataPrice30d, particleDataPrice200d, particleDataPrice1y]

错误

(node:9003) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
    at marketDataUSD.forEach ()
    at Array.forEach (<anonymous>)
    at particlePriceDataAll.forEach ()
    at Array.forEach (<anonymous>)
    at separateInformationToObjects ()
    at getSparklineData.then.sparklineTimeFrameResults ()
(node:9003) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9003) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

  • 我很确定堆栈跟踪比您提供的更详细
  • coin.marketDataUSDPricesObjectPropertiescoin.marketDataUSDRelativeObjectProperties 均未定义。
  • 数组particlePriceDataAll里面是什么
  • @Aplet123 为什么它们没有定义?我已将它们添加到函数参数中
  • @Sam 我已经用particlePriceDataAll 的数组数据编辑了我的问题

标签: javascript node.js arrays foreach


【解决方案1】:

在第 7 行和第 8 行的函数代码中,您需要执行 coin[marketDataUSDPricesObjectProperties[index]]coin[marketDataUSDRelativeObjectProperties[index]]

示例:您希望使用x 将值为world 的属性hello 添加到obj 变量中。

不要这样做:

var obj = {}, x = 'hello';
obj.x = 'world';

console.log(obj);

// Output: { x: 'hello' }

这样做:

var obj = {}, x = 'hello';
obj[x] = 'world';

console.log(obj);

// Output: { hello: 'world' }

【讨论】:

    猜你喜欢
    • 2017-10-16
    • 2014-09-10
    • 2015-10-09
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多