JavaScript Destructuring Object in JS - Nested Object object destructuring nested,destructuring nested object javascript,destructuring nested object

How can I handle destructuring a nested object that might have an undefined internal object?

For example.

I have a xhr req that returns {person: user: {}}

For example: const xhrResponse = {person: user: 'hi'}}

I am only interested in the user portion so I destruct.

const {person: {user}} = xhrResponse

console.log(user) => 'hi'

However what if the api returns {person: null}

My destructuring fails with Cannot match against 'undefined' or 'null'.

I have a solution but I feel like there might be a more elegant solution.

Solution

const handleResponse(res) => res.person ? res : {person: user: {}}

Is there a better way to handle this? Or even another way, the use of the word better is sometimes synonymous with opinion :D

More Complete Use Case:

$q.all([somePromise, someOtherPromise, getPersonPromise])
  .then(promises => [vm.promise1, vm.promise2, {person: {user: vm.user}] = promises);
Answer:1

You can add default values for nested stuff as well:

const handleResponse = ({ person: { user = {}} = {}} = {}) => {
  console.log(user);
}

handleResponse({});

(i'm assuming you using es6 based on your arrow functions)

edit: sry, only works with undefined values, but not if a value is null will leave the answer in here anyway as it might be useful to other readers

Answer:2

I have multiple DOM elememts. When each one becomes visible within the window then I fire an animation on that element. Each element registers the window scroll event to the same event handler, ...

I have multiple DOM elememts. When each one becomes visible within the window then I fire an animation on that element. Each element registers the window scroll event to the same event handler, ...

This is the JSON i have { "version": "5.2", "user_type": "online", "user": [ { "name": "John", "id": 50 }, { "name": "Mark", ...

This is the JSON i have { "version": "5.2", "user_type": "online", "user": [ { "name": "John", "id": 50 }, { "name": "Mark", ...

  1. json html table example
  2. json html table
  3. json html table javascript
  4. json html table using jquery
  5. json html table with filter
  6. json html table jquery
  7. json html table online
  8. json to html table python
  9. json to html table php
  10. json to html table c#
  11. json to html table java
  12. json into html table
  13. json to html table converter
  14. json to html table node js
  15. json to html table js
  16. json data html table
  17. json 2 html table
  18. json to html table library
  19. json to html table code
  20. json to html table node

I've been working in a legacy code that have a certain functionality that should return a random item for array and I see the following expression: array[Math.random() * arr.length | 0] My first ...

I've been working in a legacy code that have a certain functionality that should return a random item for array and I see the following expression: array[Math.random() * arr.length | 0] My first ...

HTML5 Workers can close themselves by calling the close() method. Is there a way to tell if the worker process is alive/running or if it has been closed? var myWorker = new Worker("worker.js"); ... ...

HTML5 Workers can close themselves by calling the close() method. Is there a way to tell if the worker process is alive/running or if it has been closed? var myWorker = new Worker("worker.js"); ... ...