简体   繁体   English

如何遍历对象数组并从对象中获取值-ES6

[英]how to loop over array of objects and get values from object- ES6

I need to loop over this and add this to form data.我需要循环这个并将它添加到表单数据中。

"SHOCKS" = [{
  $type: "Scenar",
  CCY_PAIR: "GBPUSD",
  D0: 0.3,
  D1: 0.3,
  …
}, {
  $type: "Scenar",
  CCY_PAIR: "KRWUSD",
  D0: 0.3,
  D1: 0.3,
  …
}, {
  $type: "Scenar",
  CCY_PAIR: "AUDJPY",
  D0: 0.08,
  D1: 0.08,
  …
} {
  $type: "Scenar",
  CCY_PAIR: "AUDKRW",
  D0: 0.08,
  D1: 0.08,
  …
}, {
  $type: "Scenar",
  CCY_PAIR: "AUDMXN",
  D0: 0.08,
  D1: 0.08,
  …
}],

I'm doing like below.我正在像下面那样做。

let FxShock = FxList["FXVOLBUMP"].SHOCKS;
let FxShocksAll1 = Object.entries(FxShock).forEach(([key, value]) => console.log(`${key}: ${value}`));

But I'm getting undefined .但我越来越undefined

Loop through the array, and then through the key -> value pairs of each object:遍历数组,然后遍历每个 object 的键 -> 值对:

 let SHOCKS = [{ $type: "Scenar", CCY_PAIR: "GBPUSD", D0: 0.3, D1: 0.3, }, { $type: "Scenar", CCY_PAIR: "KRWUSD", D0: 0.3, D1: 0.3, }, { $type: "Scenar", CCY_PAIR: "AUDJPY", D0: 0.08, D1: 0.08, }, { $type: "Scenar", CCY_PAIR: "AUDKRW", D0: 0.08, D1: 0.08, }, { $type: "Scenar", CCY_PAIR: "AUDMXN", D0: 0.08, D1: 0.08, }]; SHOCKS.forEach((obj, idx) => { console.log("object " + (idx + 1)); for (var key in obj) { if (obj.hasOwnProperty(key)) { console.log(key + " -> " + obj[key]); } } });

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

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