Questions tagged [optional-chaining]

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null. Use for questions about such chaining with a appropriate language tag like [swift], [JavaScript], etc.

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null.

JavaScript Optional chaining operator - ?.

173 questions
0
votes
1 answer

Mockito bug when testing Optional.map()?

Here is my implementation: List fetch(String input) { return Optional.ofNullable(input) .map(client::getCalendars) .orElse(repository.fetch()); } And the test: @Test void fetchGetsDataFromHolidayClient() { …
0
votes
4 answers

Optional chaining on Array.find in JS

In JavaScript, is it possible to do something akin to an optional chain on Array.find? I.e., like in the (incorrect) example below? let myArray = [{name: "foo", value: 30}, {name: "abc", value: 40}]; myArray.find(entry => entry.name ===…
0
votes
1 answer

problem with selector in react.js . If i use selector in react.js and if i export in one of the component it shows error in that selector part

export const initialState = { basket: [], }; // reducer takes care about adding and removing items from the basket // selector export const getBasketTotal = (basket) => basket?.reduce((amount, item) => item.price + amount, 0); // the above…
0
votes
2 answers

Why is it necessary to use the dot operator before the optional chaining - before a function?

So this is the pic so as you can see why do I have to use the dot operator after trying to do the optional chaining ? I tried to exclude it but then it shows error. Any help is appreciated.
Regexes
  • 105
  • 8
0
votes
0 answers

VS Code is not accepting Optional Chaining operator in Node.js code

I'm using Node.js 14.15.1 in VS Code and it is showing an error when I'm trying to use the optional chaining operator. I am not using any other dependency apart from Express. The line of code is fairly…
0
votes
1 answer

How add new field into reducer using Immer.js?

For example, state = { data: {} } How can I add a new nested field into an object? I cannot set that field, because have an error Cannot read property 'date' of undefined const reducer = produce((draft, action) => { switch (action.type) { …
0
votes
3 answers

Optional chaining for dynamic strings as property in JS

I have following sample code: var x = [ {input1: "aaa"}, {input2: "bbb"}, {input444: "ddd"}, {input55: "eee"} {input3: "ccc"}, ] I am trying to get values of props which if exists in the object something like x.forEach((item, index) => { …
Mandeep Singh
  • 1,287
  • 14
  • 34
0
votes
1 answer

combination of OPTIONAL CHAINING and NULLISH COALESCING operator not rendering the expected result

i am just learning about this combo of Optional chain and Nullish coalescing. Here is the object const restaurant = { name_: 'Classico Italiano', location: 'Via Angelo Tavanti 23, Firenze, Italy', categories: ['Italian', 'Pizzeria', 'Vegetarian',…
user13899380
0
votes
1 answer

Typescript optional chaining with check on the last argument

I have an if statement that looks like this: if ("arg3" in arg1.arg2 && customFunction(arg1.arg2.arg3)) {} Now any of the arguments can be undefined so I wanted to use optional chaining: if ("arg3" in arg1?.arg2? && customFunction(arg1.arg2.arg3))…
Sean A.S. Mengis
  • 129
  • 2
  • 13
0
votes
0 answers

Due to Nullish Coalescing and Optional Chaining operators, on VS Code's terminal appear warnings

When I implement any of the aforementioned operators in code, there are appear warnings. But code runs without any problem though. My question is, what would be able to cause such a weirdness? PS: VScode version: 1.53.2
0
votes
1 answer

Which truthy values have no property?

I am trying to write the shortest readable code to access the property of a variable that I have no information about, but I want this property access to be safe: const x = getXSomehow(); const y = x.z; // 1 const y = x?.z; // 2 const y = x != null…
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
0
votes
1 answer

syntax error while trying to use Optional Chaining (?.)

I have the following simple literal object: const ob = { 'xxx:': 'abc', aaaa: 3, ccc: [1, 2, 3] }; let's say i'm not sure that i have a ccc key in the object so i want to use Optional Chaining but i'm getting a syntax error when trying to…
Eitanos30
  • 1,331
  • 11
  • 19
0
votes
1 answer

Why is optional chaining not working in my Node REPL?

I literally copy pasted example code from MDN but optional chaining wont work in my node(v12.13.0) REPL. Throws out a syntax error saying that the dot after the question mark is invalid. What's going on ? I have already used this expression in a…
Harsha Limaye
  • 915
  • 9
  • 29
0
votes
0 answers

Is there a proposal for optional chaining usable with setting properties

Assigning a variable using optional chaining? asked the question about setting properties in a manner similar to optional chaining, e.g., foo?.bar?.baz?.quux = 'quux' . I'd like to ask more specifically if there is a proposal in the works to…
Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
0
votes
3 answers

Assign Promise To Variable In React

I'm very new to JavaScript/React in general and struggling with the concept of Promise and async. First I have getSimById, an API call in a JS file which returns a Promise: export function getSimById(simId) { return fetch(simsUrl + "/results/" +…
clattenburg cake
  • 1,096
  • 3
  • 19
  • 40