簡體   English   中英

如何遍歷 javascript 中對象內的數組元素

[英]How to iterate over array elements inside objects in javascript

我有一個 graphData 數組,我想將從 finhub 獲得的這個響應數據的收盤價數組值推送到 graphData 的 x 鍵中,並將時間戳推送到 graphData 數組的 y 鍵中。

所以它會是這樣的:graphData[{x:timestamp, y:closure price}]

響應數據:

 [{…}]0: {name: 'AMZN', c: Array(2061), h: Array(2061), l: Array(2061), o: Array(2061), …}c: (2061) [3514.605, 3513.325, 3513.64, 3514.11, 3515.21, 3517.26, 3520.43, 3519.41, 3519.1, 3517.11, 3514.25, 3513.325, 3513.55, 3513.21, 3512.87, 3510.65, 3511.34, 3510.22, 3511.505, 3513.91, 3516.6, 3517, 3516.65, 3516.02, 3513.76, 3512.04, 3511.46, 3510.3, 3509.66, 3508.88, 3507.7, 3507.43, 3506.82, 3510.93, 3509.935, 3511.11, 3512.35, 3512.71, 3511.45, 3511.47, 3509.29, …]l: (2061) [ …]s: "ok" t: (2061) [0, 1631027100, 1631027160, 1631027220, 1631027280, 1631027340, 1631027400, 1631027460, 1631027520, 1631027580, 1631027640, 1631027700, 1631027760, 1631027820, 1631027880, 1631027940, 1631028000, 1631028060, 1631028120, 1631028180, 1631028240, …]v: (2061) [ 5536, 10798, 4940, 7008, 3136, 3155, 3259, 4217, 1718, 3665, 1299, …][[Prototype]]: Object1: {name: 'RGC', c: Array(606), h: Array(606), l: Array(606), o: Array(606), …}2: {name: 'TOI', s: 'no_data'}3: {name: 'MSFT', c: Array(2531), h: Array(2531), l: Array(2531), o: Array(2531), …}4: {name: 'TSLA', c: Array(3053), h: Array(3053), l: Array(3053), o: Array(3053), …}5: {name: 'NIO', c: Array(3645), h: Array(3645), l: Array(3645), o: Array(3645), …}6: {name: 'AMC', c: Array(4152), h: Array(4152), l: Array(4152), o: Array(4152), …}length: 7[[Prototype]]: Array(0)

LineGraph.js:36 (2) [{…}, {…}]
LineGraph.js:36 (3) [{…}, {…}, {…}]
LineGraph.js:36 (4) [{…}, {…}, {…}, {…}]
LineGraph.js:36 (5) [{…}, {…}, {…}, {…}, {…}]
LineGraph.js:36 (6) [{…}, {…}, {…}, {…}, {…}, {…}]
LineGraph.js:36 (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]

所以“t”是時間戳,c 是收盤價

所以本質上我想讓graphData的x值成為每只股票的時間戳,把graphData的y值作為每只股票的收盤價,這樣我就可以繪制它了

這是我從 chartjs 使用的折線圖:

return (
<div className="linegraph">
  <Line
    data={{
      datasets: [
        {
          type: "line",
          backgroundColor: "black",
          borderColor: "#5AC53B",
          borderWidth: 2,
          pointBorderColor: "rgba(0, 0, 0, 0)",
          pointBackgroundColor: "rgba(0, 0, 0, 0)",
          pointHoverBackgroundColor: "#5AC53B",
          pointHoverBorderColor: "#000000",
          pointHoverBorderWidth: 4,
          pointHoverRadius: 6,
          data: graphData,
        },
      ],
    }}
    options={{
      maintainAspectRatio: false,
      legend: {
        display: false,
      },
      tooltips: {
        mode: "index",
        intersect: false,
      },
      scales: {
        yAxes: [
          {
            gridLines: {
              display: false,
            },
          },
        ],
      },
    }}
  />
</div>

); }

任何形式的幫助將不勝感激。 如果我不能正確地表達這個問題,我很抱歉。 英語不是我的第一語言,如果您需要任何其他信息來正確理解我,請詢問

response是您的響應,其中包含帶有 n 個元素的c和帶有 n 個元素的t

要獲得您想要的結果,您可以執行以下操作:

const data = response.t.reduce((acc, curr, i) => {
  acc.push({
    x: response.c[i],
    y: curr,
  });
  return acc;
}, []);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM