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
:
JavaScript
x
13
13
1
result.map((item) => {
2
this.virtualWallets.push({
3
credit: item.credit,
4
type: item.type,
5
name: item.name,
6
symbol: item.symbol,
7
image: item.image,
8
address: item.address,
9
address_tag: item.address_tag,
10
auto_transfer: item.auto_transfer,
11
});
12
});
13
Advertisement
Answer
Try this code:
JavaScript
1
14
14
1
result.map((item) => {
2
const {credit_formatted, type, name, symbol, image, address, address_tag, auto_transfer} = item;
3
this.virtualWallets.push({
4
credit: credit_formatted,
5
type: type,
6
name: name,
7
symbol: symbol,
8
image: image,
9
address: address,
10
address_tag: address_tag,
11
auto_transfer: auto_transfer,
12
});
13
});
14
or this:
JavaScript
1
13
13
1
result.map(({credit_formatted, type, name, symbol, image, address, address_tag, auto_transfer}) => {
2
this.virtualWallets.push({
3
credit: credit_formatted,
4
type: type,
5
name: name,
6
symbol: symbol,
7
image: image,
8
address: address,
9
address_tag: address_tag,
10
auto_transfer: auto_transfer,
11
});
12
});
13
And then you can remove unnecessary words, like this:
JavaScript
1
13
13
1
result.map(({credit_formatted, type, name, symbol, image, address, address_tag, auto_transfer}) => {
2
this.virtualWallets.push({
3
credit: credit_formatted,
4
type,
5
name,
6
symbol,
7
image,
8
address,
9
address_tag,
10
auto_transfer,
11
});
12
});
13