Array of objects return object when condition matchedJavascript: How to filter object array based on attributes?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?Sort array of objects by string property valueevent.preventDefault() vs. return falseHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?How do I return the response from an asynchronous call?
What's that red-plus icon near a text?
Today is the Center
Alternative to sending password over mail?
NMaximize is not converging to a solution
A case of the sniffles
tikz convert color string to hex value
Are the number of citations and number of published articles the most important criteria for a tenure promotion?
LaTeX: Why are digits allowed in environments, but forbidden in commands?
RSA: Danger of using p to create q
Replacing matching entries in one column of a file by another column from a different file
What does the "remote control" for a QF-4 look like?
LWC SFDX source push error TypeError: LWC1009: decl.moveTo is not a function
How old can references or sources in a thesis be?
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
Does an object always see its latest internal state irrespective of thread?
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Perform and show arithmetic with LuaLaTeX
Roll the carpet
Important Resources for Dark Age Civilizations?
Intersection point of 2 lines defined by 2 points each
I'm flying to France today and my passport expires in less than 2 months
How does quantile regression compare to logistic regression with the variable split at the quantile?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
Why do I get two different answers for this counting problem?
Array of objects return object when condition matched
Javascript: How to filter object array based on attributes?What is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to append something to an array?Sort array of objects by string property valueevent.preventDefault() vs. return falseHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?How to use foreach with array in JavaScript?How do I return the response from an asynchronous call?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an array which have value of id, email and password.
let array = [
id: hyu, email: a@a.com, password: 123,
id: rft, email: b@b.com, password: 456,
id: ght, email: c@c.com, password: 789,
id: kui, email: d@d.com, password: 679
]
Now I would like to return that object when my condition is matched. For that I created a function using javascript some
function but I want to return the object and we know that some
function return boolean.
I don't have any idea how to do this.
My code is:
const isEmailExists = (email, array) =>
return array.some(function(el)
return el.email === email;
);
;
if (isEmailExists("c@c.com", array) == true)
// I want to work here with returned objects i.e on success case
else
// error case
Any help is really appreciated
javascript ecmascript-6
add a comment |
I have an array which have value of id, email and password.
let array = [
id: hyu, email: a@a.com, password: 123,
id: rft, email: b@b.com, password: 456,
id: ght, email: c@c.com, password: 789,
id: kui, email: d@d.com, password: 679
]
Now I would like to return that object when my condition is matched. For that I created a function using javascript some
function but I want to return the object and we know that some
function return boolean.
I don't have any idea how to do this.
My code is:
const isEmailExists = (email, array) =>
return array.some(function(el)
return el.email === email;
);
;
if (isEmailExists("c@c.com", array) == true)
// I want to work here with returned objects i.e on success case
else
// error case
Any help is really appreciated
javascript ecmascript-6
You're looking for.filter()
.
– Madara Uchiha♦
2 days ago
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
2 days ago
add a comment |
I have an array which have value of id, email and password.
let array = [
id: hyu, email: a@a.com, password: 123,
id: rft, email: b@b.com, password: 456,
id: ght, email: c@c.com, password: 789,
id: kui, email: d@d.com, password: 679
]
Now I would like to return that object when my condition is matched. For that I created a function using javascript some
function but I want to return the object and we know that some
function return boolean.
I don't have any idea how to do this.
My code is:
const isEmailExists = (email, array) =>
return array.some(function(el)
return el.email === email;
);
;
if (isEmailExists("c@c.com", array) == true)
// I want to work here with returned objects i.e on success case
else
// error case
Any help is really appreciated
javascript ecmascript-6
I have an array which have value of id, email and password.
let array = [
id: hyu, email: a@a.com, password: 123,
id: rft, email: b@b.com, password: 456,
id: ght, email: c@c.com, password: 789,
id: kui, email: d@d.com, password: 679
]
Now I would like to return that object when my condition is matched. For that I created a function using javascript some
function but I want to return the object and we know that some
function return boolean.
I don't have any idea how to do this.
My code is:
const isEmailExists = (email, array) =>
return array.some(function(el)
return el.email === email;
);
;
if (isEmailExists("c@c.com", array) == true)
// I want to work here with returned objects i.e on success case
else
// error case
Any help is really appreciated
javascript ecmascript-6
javascript ecmascript-6
asked 2 days ago
AksAks
415
415
You're looking for.filter()
.
– Madara Uchiha♦
2 days ago
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
2 days ago
add a comment |
You're looking for.filter()
.
– Madara Uchiha♦
2 days ago
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
2 days ago
You're looking for
.filter()
.– Madara Uchiha♦
2 days ago
You're looking for
.filter()
.– Madara Uchiha♦
2 days ago
4
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
2 days ago
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
2 days ago
add a comment |
5 Answers
5
active
oldest
votes
You can make use of .filter()
and check if the filtered array has a length of more than 0
:
let array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
let filtered = array.filter(row => row.email === 'a@a.com');
console.log(filtered);
if (filtered.length > 0) /* mail exists */
else /* mail does not exist */
add a comment |
Assuming the email is unique, you can use find()
. This will return null
if not email does not exist.
let array = ["id":"hyu","email":"a@a.com","password":123,"id":"rft","email":"b@b.com","password":456,"id":"ght","email":"c@c.com","password":789,"id":"kui","email":"d@d.com","password":679];
const getObject = (email, array) => null;
;
console.log(getObject("c@c.com", array));
Shorter Version:
const getObject = (email, array) => array.find(el => el.email === email ) || null;
add a comment |
You could take a function and hand over the key and the value and filter the array.
function getObjects(array, key, value)
return array.filter(object => object[key] === value);
let array = [ id: 'hyu', email: 'a@a.com', password: 123 , id: 'rft', email: 'b@b.com', password: 456 , id: 'ght', email: 'c@c.com', password: 789 , id: 'kui', email: 'd@d.com', password: 679 ];
console.log(getObjects(array, 'email', 'c@c.com'));
add a comment |
Use find()
, not filter()
, when you expect zero or one match. find()
will return undefined
if no match is found and since undefined
is falsy and any object is truthy, you can use the return value as a condition for an if
clause directly.
const array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
const mail = array.find(row => row.email === 'a@a.com');
if (mail)
// Do stuff with mail
console.log(mail);
else
// Handle error
add a comment |
const found = array.find(item => (item.email === 'a@a.com')) || null;
console.log(found);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55491211%2farray-of-objects-return-object-when-condition-matched%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can make use of .filter()
and check if the filtered array has a length of more than 0
:
let array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
let filtered = array.filter(row => row.email === 'a@a.com');
console.log(filtered);
if (filtered.length > 0) /* mail exists */
else /* mail does not exist */
add a comment |
You can make use of .filter()
and check if the filtered array has a length of more than 0
:
let array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
let filtered = array.filter(row => row.email === 'a@a.com');
console.log(filtered);
if (filtered.length > 0) /* mail exists */
else /* mail does not exist */
add a comment |
You can make use of .filter()
and check if the filtered array has a length of more than 0
:
let array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
let filtered = array.filter(row => row.email === 'a@a.com');
console.log(filtered);
if (filtered.length > 0) /* mail exists */
else /* mail does not exist */
You can make use of .filter()
and check if the filtered array has a length of more than 0
:
let array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
let filtered = array.filter(row => row.email === 'a@a.com');
console.log(filtered);
if (filtered.length > 0) /* mail exists */
else /* mail does not exist */
let array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
let filtered = array.filter(row => row.email === 'a@a.com');
console.log(filtered);
if (filtered.length > 0) /* mail exists */
else /* mail does not exist */
let array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
let filtered = array.filter(row => row.email === 'a@a.com');
console.log(filtered);
if (filtered.length > 0) /* mail exists */
else /* mail does not exist */
edited 2 days ago
answered 2 days ago
Sebastian KaczmarekSebastian Kaczmarek
2,0141025
2,0141025
add a comment |
add a comment |
Assuming the email is unique, you can use find()
. This will return null
if not email does not exist.
let array = ["id":"hyu","email":"a@a.com","password":123,"id":"rft","email":"b@b.com","password":456,"id":"ght","email":"c@c.com","password":789,"id":"kui","email":"d@d.com","password":679];
const getObject = (email, array) => null;
;
console.log(getObject("c@c.com", array));
Shorter Version:
const getObject = (email, array) => array.find(el => el.email === email ) || null;
add a comment |
Assuming the email is unique, you can use find()
. This will return null
if not email does not exist.
let array = ["id":"hyu","email":"a@a.com","password":123,"id":"rft","email":"b@b.com","password":456,"id":"ght","email":"c@c.com","password":789,"id":"kui","email":"d@d.com","password":679];
const getObject = (email, array) => null;
;
console.log(getObject("c@c.com", array));
Shorter Version:
const getObject = (email, array) => array.find(el => el.email === email ) || null;
add a comment |
Assuming the email is unique, you can use find()
. This will return null
if not email does not exist.
let array = ["id":"hyu","email":"a@a.com","password":123,"id":"rft","email":"b@b.com","password":456,"id":"ght","email":"c@c.com","password":789,"id":"kui","email":"d@d.com","password":679];
const getObject = (email, array) => null;
;
console.log(getObject("c@c.com", array));
Shorter Version:
const getObject = (email, array) => array.find(el => el.email === email ) || null;
Assuming the email is unique, you can use find()
. This will return null
if not email does not exist.
let array = ["id":"hyu","email":"a@a.com","password":123,"id":"rft","email":"b@b.com","password":456,"id":"ght","email":"c@c.com","password":789,"id":"kui","email":"d@d.com","password":679];
const getObject = (email, array) => null;
;
console.log(getObject("c@c.com", array));
Shorter Version:
const getObject = (email, array) => array.find(el => el.email === email ) || null;
let array = ["id":"hyu","email":"a@a.com","password":123,"id":"rft","email":"b@b.com","password":456,"id":"ght","email":"c@c.com","password":789,"id":"kui","email":"d@d.com","password":679];
const getObject = (email, array) => null;
;
console.log(getObject("c@c.com", array));
let array = ["id":"hyu","email":"a@a.com","password":123,"id":"rft","email":"b@b.com","password":456,"id":"ght","email":"c@c.com","password":789,"id":"kui","email":"d@d.com","password":679];
const getObject = (email, array) => null;
;
console.log(getObject("c@c.com", array));
answered 2 days ago
EddieEddie
19.6k51642
19.6k51642
add a comment |
add a comment |
You could take a function and hand over the key and the value and filter the array.
function getObjects(array, key, value)
return array.filter(object => object[key] === value);
let array = [ id: 'hyu', email: 'a@a.com', password: 123 , id: 'rft', email: 'b@b.com', password: 456 , id: 'ght', email: 'c@c.com', password: 789 , id: 'kui', email: 'd@d.com', password: 679 ];
console.log(getObjects(array, 'email', 'c@c.com'));
add a comment |
You could take a function and hand over the key and the value and filter the array.
function getObjects(array, key, value)
return array.filter(object => object[key] === value);
let array = [ id: 'hyu', email: 'a@a.com', password: 123 , id: 'rft', email: 'b@b.com', password: 456 , id: 'ght', email: 'c@c.com', password: 789 , id: 'kui', email: 'd@d.com', password: 679 ];
console.log(getObjects(array, 'email', 'c@c.com'));
add a comment |
You could take a function and hand over the key and the value and filter the array.
function getObjects(array, key, value)
return array.filter(object => object[key] === value);
let array = [ id: 'hyu', email: 'a@a.com', password: 123 , id: 'rft', email: 'b@b.com', password: 456 , id: 'ght', email: 'c@c.com', password: 789 , id: 'kui', email: 'd@d.com', password: 679 ];
console.log(getObjects(array, 'email', 'c@c.com'));
You could take a function and hand over the key and the value and filter the array.
function getObjects(array, key, value)
return array.filter(object => object[key] === value);
let array = [ id: 'hyu', email: 'a@a.com', password: 123 , id: 'rft', email: 'b@b.com', password: 456 , id: 'ght', email: 'c@c.com', password: 789 , id: 'kui', email: 'd@d.com', password: 679 ];
console.log(getObjects(array, 'email', 'c@c.com'));
function getObjects(array, key, value)
return array.filter(object => object[key] === value);
let array = [ id: 'hyu', email: 'a@a.com', password: 123 , id: 'rft', email: 'b@b.com', password: 456 , id: 'ght', email: 'c@c.com', password: 789 , id: 'kui', email: 'd@d.com', password: 679 ];
console.log(getObjects(array, 'email', 'c@c.com'));
function getObjects(array, key, value)
return array.filter(object => object[key] === value);
let array = [ id: 'hyu', email: 'a@a.com', password: 123 , id: 'rft', email: 'b@b.com', password: 456 , id: 'ght', email: 'c@c.com', password: 789 , id: 'kui', email: 'd@d.com', password: 679 ];
console.log(getObjects(array, 'email', 'c@c.com'));
answered 2 days ago
Nina ScholzNina Scholz
196k15108179
196k15108179
add a comment |
add a comment |
Use find()
, not filter()
, when you expect zero or one match. find()
will return undefined
if no match is found and since undefined
is falsy and any object is truthy, you can use the return value as a condition for an if
clause directly.
const array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
const mail = array.find(row => row.email === 'a@a.com');
if (mail)
// Do stuff with mail
console.log(mail);
else
// Handle error
add a comment |
Use find()
, not filter()
, when you expect zero or one match. find()
will return undefined
if no match is found and since undefined
is falsy and any object is truthy, you can use the return value as a condition for an if
clause directly.
const array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
const mail = array.find(row => row.email === 'a@a.com');
if (mail)
// Do stuff with mail
console.log(mail);
else
// Handle error
add a comment |
Use find()
, not filter()
, when you expect zero or one match. find()
will return undefined
if no match is found and since undefined
is falsy and any object is truthy, you can use the return value as a condition for an if
clause directly.
const array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
const mail = array.find(row => row.email === 'a@a.com');
if (mail)
// Do stuff with mail
console.log(mail);
else
// Handle error
Use find()
, not filter()
, when you expect zero or one match. find()
will return undefined
if no match is found and since undefined
is falsy and any object is truthy, you can use the return value as a condition for an if
clause directly.
const array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
const mail = array.find(row => row.email === 'a@a.com');
if (mail)
// Do stuff with mail
console.log(mail);
else
// Handle error
const array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
const mail = array.find(row => row.email === 'a@a.com');
if (mail)
// Do stuff with mail
console.log(mail);
else
// Handle error
const array = [
id: 'hyu', email: 'a@a.com', password: 123,
id: 'rft', email: 'b@b.com', password: 456,
id: 'ght', email: 'c@c.com', password: 789,
id: 'kui', email: 'd@d.com', password: 679
]
const mail = array.find(row => row.email === 'a@a.com');
if (mail)
// Do stuff with mail
console.log(mail);
else
// Handle error
answered 2 days ago
JollyJokerJollyJoker
22115
22115
add a comment |
add a comment |
const found = array.find(item => (item.email === 'a@a.com')) || null;
console.log(found);
add a comment |
const found = array.find(item => (item.email === 'a@a.com')) || null;
console.log(found);
add a comment |
const found = array.find(item => (item.email === 'a@a.com')) || null;
console.log(found);
const found = array.find(item => (item.email === 'a@a.com')) || null;
console.log(found);
answered 2 days ago
0xsr3k4nth0xsr3k4nth
778
778
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55491211%2farray-of-objects-return-object-when-condition-matched%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You're looking for
.filter()
.– Madara Uchiha♦
2 days ago
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
2 days ago