2

I have an object like this

const obj = [{a: 'a'}];

I can get it like: const { a } = obj[0]; //a

but what if obj['x'] doesn't exist?

I tried this with optional chainning but doesn't seem to work. const { a } = obj?.[1];

3
  • As you're trying to destructure it, it still won't help (obj?.[1] returns just as undefined as obj[1] would), you should either drop the destructuring and use a simple property access (with ?. where needed) or use destructuring everywhere with default values. Commented Apr 16, 2021 at 8:04
  • To add to @hgb123 answer. If you wan to use null as default value instead of undefined you can just use const {a = null} = obj?.[1] || {}. Commented Apr 16, 2021 at 8:09
  • @keysl will this worked the same? const a = obj?.[1]?.a Commented Apr 16, 2021 at 8:14

4 Answers 4

5

You are close to it. You should make sure to fallback with an empty object, for the destructing later to make sense

const obj = [{a: 'a'}];
const { a } = obj?.[1] || {};

console.log(a)

Update

This would be even shorter if you don't use destructing in this case

const obj = [{a: 'a'}];
const a = obj?.[1]?.a; // or: obj?.[1]?.['a']

console.log(a)

Sign up to request clarification or add additional context in comments.

8 Comments

With || {} there's no need for optional chaining o.O
@Andreas There is. Optional chaining is concerned with obj being undefined, OR operator is concerned what will be returned with obj[1] if obj[1] is undefined.
@RobertoZvjerković "but what if obj['x'] doesn't exist?" - So obj exists but obj.x may not. There's no need for optional chaining.
what if I avoid using destructing? const a = obj?.[1]?.a, do I still need a fallback?
@Andreas Except you will get a runtime error if you don't use optional chaining, and obj doesn't exist
|
1

You could festructure with an index as computed property and a default object for a missing item of the array.

const
    obj = [{ a: 'a' }],
    { [2]: { a } = {} } = obj;

console.log(a);

1 Comment

omg this is so complicated, what if I use multiple ? like const a = obj?.[1]?.a
1

Take a look at the following three cases, where I've used optional chaining.

You need to first check if the first element of the array (index 0) exists and then check if there's a field a in it.

// Case 1:
const obj1 = [{ a: "a" }];
console.log(obj1?.[0]?.a); // "a"

// Case 2:
const obj2 = [{ b: "b" }];
console.log(obj2?.[0]?.a); // undefined

// Case 3:
const obj3 = [];
console.log(obj3?.[0]?.a); // undefined

1 Comment

@JudyAllen Please check if this solves your problem, let me know if you have any issues.
0

You can use the Logical OR operator.

const { a } = obj[x] || {}

If obj does not have a property x, a will be set to undefined.

1 Comment

will this shorter? const a = obj?.[1]?.a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.