使用 for/in 创建的 JS 对象多次返回相同的对象到数组

JS Object created with for/in returning the same object to the array multiple times

我有一个对象数据如下所示:

{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "IBM",
        "3. Last Refreshed": "2021-09-09 18:05:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2021-09-09 18:05:00": {
            "1. open": "137.8300",
            "2. high": "137.8300",
            "3. low": "137.8300",
            "4. close": "137.8300",
            "5. volume": "104"
        },
        "2021-09-09 17:25:00": {
            "1. open": "137.7500",
            "2. high": "137.7500",
            "3. low": "137.7500",
            "4. close": "137.7500",
            "5. volume": "478"
        },
        "2021-09-09 16:30:00": {
            "1. open": "137.8000",
            "2. high": "137.8000",
            "3. low": "137.8000",
            "4. close": "137.8000",
            "5. volume": "459"
        },
    }
}

这是我的函数:

const myfunction = () => {
  var newArray = []
  var newObject = {}
  var firstobj = myobject['Time Series (5min)']

  for (var key in firstobj) {
    console.log(key)
    newObject.x = key.substr(11, 19)
    newObject.y = myobject['Time Series (5min)'][`${key}`]['4. close']

    newArray.push(newObject)
  }
  return newArray
}

console.log(myfunction())

使用 for/in 创建的 JS 对象 return 将同一对象多次写入数组。我不确定为什么我看不到错误,或者我哪里出错了。有谁知道我应该如何 return 将对象发送到 newArray 以便创建 {x: 1234, y: 1234} 的列表。 (只是示例数字,一起查看此视图​​代码/控制台附带的图像。控制台的第一部分是记录键的结尾,控制台的第二部分是对象数组日志的开始,[{x: 1234, y: 1234}, {x: 1234, y: 1234}]我愿意return!

结果需要是:

[{ x: '15:50:00', y: '138.3550' },
 { x: '15:55:00', y: '134.3250' }]

不是:

[{ x: '15:50:00', y: '138.3550' },
 { x: '15:50:00', y: '138.3550' }]

PS:我已将结构 return 编辑为数组,而不是对象数组。我只是想说我正确地调用了 API,只是可能没有正确引用它。

PPS: 如果有更简单的方法,请告诉我你会怎么做!

您将 newArray 推满了对同一对象的引用。

const obj = { x: 0, y: 0 };
const newArray = [obj];

obj.x = 1;
newArray.push(obj);

// since index 0 and 1 refer to the same object they will now "both" be
// { x: 1, y: 0 }

因此,与其使用单个对象,不如创建一个副本,然后更新属性:

for (var key in firstobj) {
  console.log(key)
  const copy = { ...newObject }; // create a shallow copy

  // Setting x and y don't update newObject, and objects pushed to
  // newArray are distinct objects.
  copy.x = key.substr(11, 19)
  copy.y = myobject['Time Series (5min)'][`${key}`]['4. close']

  newArray.push(copy)
}

与其创建一个新变量来临时存储值,不如将其完全放入 push 本身,如下所示:

array.push({x: data_x, y: data_y})

这是完整的工作代码:

const data = {
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "IBM",
        "3. Last Refreshed": "2021-09-09 18:05:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2021-09-09 18:05:00": {
            "1. open": "137.8300",
            "2. high": "137.8300",
            "3. low": "137.8300",
            "4. close": "137.8300",
            "5. volume": "104"
        },
        "2021-09-09 17:25:00": {
            "1. open": "137.7500",
            "2. high": "137.7500",
            "3. low": "137.7500",
            "4. close": "137.7500",
            "5. volume": "478"
        },
        "2021-09-09 16:30:00": {
            "1. open": "137.8000",
            "2. high": "137.8000",
            "3. low": "137.8000",
            "4. close": "137.8000",
            "5. volume": "459"
        },
       }
      };



const myfunction = (myobject) => {
  var newArray = [];
  var firstobj = myobject['Time Series (5min)'];

  for (var key in firstobj) {
    console.log(key)

    newArray.push({
      x: key.substr(11, 19),
      y: myobject['Time Series (5min)'][`${key}`]['4. close']
    });
  }
  return newArray
}

console.log(myfunction(data))

这是因为 newObject 每次调用时都会更新。 所以它将最后一个保留为变量。检查这张图片。