Monday, October 17, 2022

ES6 JavaScript object destructuring

Extract array from nested object by destructuring:

{
"status": "success",
"time": "2022-10-17T17:09:50.486Z",
"result": 10,
"data": {
"posts": [ { "_id": "id", "image": "url", "link": "url",
"title": "title", "text": "text", "__v": 0, "counter": 1 } ]
}
}
const { data: { posts } = [] } = data; // extracting posts array from data

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

https://www.w3schools.com/react/react_es6_destructuring.asp

https://blog.bitsrc.io/getting-nested-value-by-using-javascript-7828f7768cc7

https://stackoverflow.com/questions/909003/getting-the-first-index-of-an-object

https://dmitripavlutin.com/javascript-object-destructuring/

https://www.deadcoderising.com/2017-03-28-es6-destructuring-an-elegant-way-of-extracting-data-from-arrays-and-objects-in-javascript/

https://itnext.io/using-es6-to-destructure-nested-objects-in-javascript-avoid-undefined-errors-that-break-your-code-612ae67913e9

No comments:

Post a Comment