Skip to content
Advertisement

destructing es6 in array push

i want to use something like destructing assignment in ES6 for having cleaner code but i dont know how to use it in something like pushing in array or anyway can is use it or something similar?. this sample code is in vue js:

result.map((item) => {
   this.virtualWallets.push({
      credit: item.credit,
      type: item.type,
      name: item.name,
      symbol: item.symbol,
      image: item.image,
      address: item.address,
      address_tag: item.address_tag,
      auto_transfer: item.auto_transfer,
   });
});

Advertisement

Answer

Try this code:

result.map((item) => {
  const {credit_formatted, type, name, symbol, image, address, address_tag, auto_transfer} = item;
  this.virtualWallets.push({
    credit: credit_formatted,
    type: type,
    name: name,
    symbol: symbol,
    image: image,
    address: address,
    address_tag: address_tag,
    auto_transfer: auto_transfer,
  });
});

or this:

result.map(({credit_formatted, type, name, symbol, image, address, address_tag, auto_transfer}) => {
  this.virtualWallets.push({
    credit: credit_formatted,
    type: type,
    name: name,
    symbol: symbol,
    image: image,
    address: address,
    address_tag: address_tag,
    auto_transfer: auto_transfer,
  });
});

And then you can remove unnecessary words, like this:

result.map(({credit_formatted, type, name, symbol, image, address, address_tag, auto_transfer}) => {
  this.virtualWallets.push({
    credit: credit_formatted,
    type,
    name,
    symbol,
    image,
    address,
    address_tag,
    auto_transfer,
  });
});
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement