繁体   English   中英

如何访问不相似的嵌套对象并遍历它们?

[英]How can I access nested objects that are not similiar and iterate through them?

我有一个到 websocket 的连接,它给我这种格式的数据。

{lastUpdateId: 2303,Bids: [[ '12312.81000000', '0.77085400' ],[ '23423.79000000', '0.02179700' ],[ '23423.76000000', '0.00550000' ],[ '23422.70000000', '0.00052800' ],[ '64536.24000000', '0.00902500' ]],}

我想将“bids” arrays 的所有主要值放入一个新的 xBids 数组中,将“bids” arrays 的次要值放入一个新的 yBids 数组中。 所以它看起来像这样。

xBids = ['12312.81000000','23423.79000000','23423.76000000','23422.70000000''64536.24000000'] yBids = ['0.77085400','0.02179700','0.00550000','0.00052800''0.00902500']

非常感谢任何帮助。

使用 function .map()返回一个新数组,其中包含从现有数组派生的值。

 const result = {lastUpdateId: 2303,Bids: [[ '12312.81000000', '0.77085400' ],[ '23423.79000000', '0.02179700' ],[ '23423.76000000', '0.00550000' ],[ '23422.70000000', '0.00052800' ],[ '64536.24000000', '0.00902500' ]]} const xbids = result.Bids.map(bid=>bid[0]); const ybids = result.Bids.map(bid=>bid[1]); console.log('xbids: ', xbids); console.log('ybids: ', ybids);

您可以使用Array#map

 const obj = {lastUpdateId: 2303,Bids: [[ '12312.81000000', '0.77085400' ],[ '23423.79000000', '0.02179700' ],[ '23423.76000000', '0.00550000' ],[ '23422.70000000', '0.00052800' ],[ '64536.24000000', '0.00902500' ]],} const arr1 = obj.Bids.map(x => x[0]), arr2 = obj.Bids.map(x => x[1]); console.log(arr1); console.log(arr2);

只需遍历一次数组即可,您可以使用Array#reduce

 const obj = {lastUpdateId: 2303,Bids: [[ '12312.81000000', '0.77085400' ],[ '23423.79000000', '0.02179700' ],[ '23423.76000000', '0.00550000' ],[ '23422.70000000', '0.00052800' ],[ '64536.24000000', '0.00902500' ]],}; const [arr1, arr2] = obj.Bids.reduce((acc,[x,y])=>(acc[0].push(x), acc[1].push(y), acc), [[], []]); console.log(arr1); console.log(arr2);

这有效(ES6 语法):

const testObj = {lastUpdateId: 2303,Bids: [[ '12312.81000000', '0.77085400' ],[ '23423.79000000', '0.02179700' ],[ '23423.76000000', '0.00550000' ],[ '23422.70000000', '0.00052800' ],[ '64536.24000000', '0.00902500' ]],};
const xBids = [];
const yBids = [];
testObj.Bids.map(bid => { xBids.push(bid[0]), yBids.push(bid[1])});

这很简单,但需要两次迭代。

let xBids = response.Bids.map(element=>{
  element[0];
}
)

let yBids = response.Bids.map(element=>{
element[1];
})

单次迭代法

let xBids= [];
let yBids = [];
response.Bids.forEach((element)=>{
xbids.push(element[0]);
yBids.push(element[1])
}
)

使用map() function 创建 2 个新的 arrays,其中包含所需的数据,如下所示。

 const data = { lastUpdateId: 2303, Bids: [ ['12312.81000000', '0.77085400'], ['23423.79000000', '0.02179700'], ['23423.76000000', '0.00550000'], ['23422.70000000', '0.00052800'], ['64536.24000000', '0.00902500'] ] }; const xBids = data.Bids.map(bid => bid[0]); const yBids = data.Bids.map(bid => bid[1]); console.log(xBids); console.log(yBids);

您也可以使用reduce在1个操作中完成此任务:

主意:

  • 创建嵌套数组结构。
  • 循环投标和每次迭代,将当前值推送到必要的数组。
  • 也为下一次迭代返回这个嵌套结构。
  • 在完成时,接收这个结构是分开的。

 const data={lastUpdateId: 2303,Bids: [[ '12312.81000000', '0.77085400' ],[ '23423.79000000', '0.02179700' ],[ '23423.76000000', '0.00550000' ],[ '23422.70000000', '0.00052800' ],[ '64536.24000000', '0.00902500' ]],} const [xBids, yBids] = data.Bids.reduce(([xBidsList, yBidsList], [x, y]) => [ [...xBidsList, x], [...yBidsList, y] ], [[], []] ) console.log(xBids, yBids)

为清楚起见的简化版本:

 const data={lastUpdateId: 2303,Bids: [[ '12312.81000000', '0.77085400' ],[ '23423.79000000', '0.02179700' ],[ '23423.76000000', '0.00550000' ],[ '23422.70000000', '0.00052800' ],[ '64536.24000000', '0.00902500' ]],} const xBids = []; const yBids = []; data.Bids.reduce((bidLists, bid) => { const xValues = bidLists[0] const yValues = bidLists[1] xValues.push(bid[0]) yValues.push(bid[1]) return [xValues, yValues] }, [ xBids, yBids ]) console.log(xBids, yBids)

暂无
暂无

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

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