简体   繁体   English

使用map从数组中解构对象?

[英]Destructuring objects from an array using map?

I have wrote this simple code that destructures an array of objects to build new arrays from each key.我写了这个简单的代码,它解构一个对象数组,从每个键构建新数组。 I am learning ES6 and would like to refactor it into one line of code using destructuring but I dont fully understand how too.我正在学习 ES6 并想使用解构将其重构为一行代码,但我也不完全理解如何。

 let candles = [{ open: 1, high: 2, low: 0.5, close: 1.5, volume: 200 }]; let open = candles.map(x => x.open); let high = candles.map(x => x.high); let low = candles.map(x => x.low); let close = candles.map(x => x.close); let volume = candles.map(x => x.volume); console.log(open, high, low, close, volume);

I am thinking it should look something along the lines of this?我在想它应该看起来像这样吗?

let [open, high, low, close, volume] = candles.map(key => key.value); 

But it is clearly wrong!但这显然是错误的! Thank you for help if someone can direct me the correct way on doing this!如果有人可以指导我正确的方法来做这件事,谢谢你的帮助!

Here's a solution using Array.prototype.reduce() :这是使用Array.prototype.reduce()的解决方案:

 const candles = [{open: 1, close: 2, low: 3, high: 4, volume: 5}, {open: 6, close: 7, low: 8, high: 9, volume: 10}]; const result = candles.reduce((a, v) => { Object.keys(v).forEach(k => (a[k] = a[k] || []).push(v[k])); return a; }, {}); console.log(result);

let candles = [ {
  open: 1,
  high: 2,
  low: 0.5,
  close: 1.5,
  volume: 200
} ]

const { open, high, low, close, volume } = candles.reduce( ( accumulator, item ) => {
    Object.keys( item ).forEach( key => {
        accumulator[ key ] = ( accumulator[ key ] || [] ).concat( item[ key ] ) 
    } )
    return accumulator
}, {} )

So you should start with reduce first and return an object.所以你应该先从reduce开始,然后返回一个对象。 Then you can use destructuring on that.然后你可以使用解构。 Here's an example:下面是一个例子:

const candles = [{
  open: 1,
  high: 2,
  low: 0.5,
  close: 1.5,
  volume: 200
}, {
  open: 2,
  high: 3,
  low: 0.6,
  close: 1.4,
  volume: 300
}];

const reduction = candles.reduce((acc, candle) => {
  for (let key in candle) {
    if (!(key in acc)) {
      acc[key] = [];
    }

    acc[key].push(candle[key]);
  }

  return acc;
}, {});

console.log(reduction);
// { open: [ 1, 2 ],
//   high: [ 2, 3 ],
//   low: [ 0.5, 0.6 ],
//   close: [ 1.5, 1.4 ],
//   volume: [ 200, 300 ] }

const {open, high, low, close, volume} = reduction;

console.log(open, high, low, close, volume);
// [ 1, 2 ] [ 2, 3 ] [ 0.5, 0.6 ] [ 1.5, 1.4 ] [ 200, 300 ]

Although map is powerful and useful it is not the most effective strategy for your example case.尽管map功能强大且有用,但对于您的示例而言,它并不是最有效的策略。

The simplest way to do this is to not use map at all.最简单的方法是根本不使用地图。 If you truly want a one-line solution try using destructuring :如果您真的想要单行解决方案,请尝试使用解构

let candles = [{
  open: 1,
  high: 2,
  low: 0.5,
  close: 1.5,
  volume: 200
}];

[{open, high, low, close, volume}] = candles

That simple one-liner is all you need:这个简单的单行是你所需要的:

console.log(open, high, low, close, volume)  // 1 2 0.5 1.5 200

Array map destructuring数组映射解构

Simple trick: [{id:1, name:'awadhesh'}].map(({id, name}) => id + '---' + name);简单的技巧: [{id:1, name:'awadhesh'}].map(({id, name}) => id + '---' + name);

let candles = [{
 open: 1,
 high: 2,
 low: 0.5,
 close: 1.5,
 volume: 200
}];
let open = candles.map(({open}) => open);
let high = candles.map(({high}) => high);
let low = candles.map(({low}) => low);
let close = candles.map(({close}) => close);
let volume = candles.map(({volume}) => volume);

console.log(open, high, low, close, volume);

log print = [1] [2] [0.5] [1.5] [200]日志打印 = [1] [2] [0.5] [1.5] [200]

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

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