2016-08-06 56 views
2

所以,我將一個對象傳遞給一個ES6函數,我想解構直到參數的參數。例如,以下代碼將記錄data支持stuff,但我希望它記錄datathings支持stuff。所以正確答案會記錄[1,2,3,4]。我知道,一點都不迷惑。任何人都知道這是可能的嗎?解構對象參數兩個級別?

const stuff = { 
    data: { 
     things: [1,2,3,4] 
    } 
}; 
const getThings = ({ data }) => { 
    console.log(data)  
}; 
getThings(stuff); 
+1

https://ponyfoo.com/文章/ ES6-解構縱深 – gcampbell

回答

7

當然,這裏的如何:

const stuff = { 
 
    data: { 
 
     things: [1,2,3,4] 
 
    } 
 
}; 
 
const getThings = ({ data: {things} }) => { 
 
    console.log(things)  
 
}; 
 
getThings(stuff);

1

正如我理解正確的話,正確的答案是:

const getThings = ({ data: { things } }) => { 
    console.log(things)  
};