Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value needed if key not exist inside some object in array of objects #64

Open
RSG6558 opened this issue Apr 14, 2020 · 4 comments

Comments

@RSG6558
Copy link

RSG6558 commented Apr 14, 2020

Hi, I am looking for a solution where my data is
[ { "name": "rahul", "email": "[email protected]" }, { "name": "amit", "email": "[email protected]" }, { "email": "[email protected]" }, { "name": "brijesh", "email": "[email protected]" } ]
Note: name key is missing in third object

query: [].name

result:
[ "rahul", "amit", "brijesh" ]

expected result:
[ "rahul", "amit", "", "brijesh" ]

please help

Thanks

@darrenmothersele
Copy link

Does this help?

map(&(name || ``), @)

Online demo: https://codesandbox.io/s/fervent-architecture-o7p7r?file=/src/index.js

@darrenmothersele
Copy link

darrenmothersele commented Apr 14, 2020

Explanation:

The & denotes an expression is to be used as a value, rather than it being evaluated.

The map function takes an expression and an array as arguments. It applies the expression to each item in the array and returns an array of the results.

And `` denotes an empty string. The expression name || `` means that if the value for name is null then the empty string is used instead.

@RSG6558
Copy link
Author

RSG6558 commented Apr 14, 2020

Hi,
thanks @darrenmothersele this solution perfectly worked for me. and thanks for explanation as well.

@nabheet
Copy link

nabheet commented Dec 15, 2022

Is this two backticks? or two single quotes? it seems both work but wanted to clarify?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants