Array of objects return object when condition matched Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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?For-each over an array in JavaScript?How do I return the response from an asynchronous call?
What is "gratricide"?
Is it possible for SQL statements to execute concurrently within a single session in SQL Server?
Drawing spherical mirrors
C's equality operator on converted pointers
Amount of permutations on an NxNxN Rubik's Cube
How to pronounce 伝統色
Can a Beast Master ranger change beast companions?
What is the meaning of 'breadth' in breadth first search?
Do wooden building fires get hotter than 600°C?
How to compare two different files line by line in unix?
Can a new player join a group only when a new campaign starts?
Should a wizard buy fine inks every time he want to copy spells into his spellbook?
How do living politicians protect their readily obtainable signatures from misuse?
Maximum summed subsequences with non-adjacent items
Is it possible to force a specific program to remain in memory after closing it?
What order were files/directories output in dir?
AppleTVs create a chatty alternate WiFi network
Significance of Cersei's obsession with elephants?
If Windows 7 doesn't support WSL, then what is "Subsystem for UNIX-based Applications"?
Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?
Lagrange four-squares theorem --- deterministic complexity
Google .dev domain strangely redirects to https
How does a spellshard spellbook work?
Do I really need to have a message in a novel to appeal to readers?
Array of objects return object when condition matched
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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?For-each over an 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♦
Apr 3 at 9:09
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
Apr 3 at 9:12
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 Apr 3 at 9:05
AksAks
415
415
You're looking for.filter()
.
– Madara Uchiha♦
Apr 3 at 9:09
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
Apr 3 at 9:12
add a comment |
You're looking for.filter()
.
– Madara Uchiha♦
Apr 3 at 9:09
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
Apr 3 at 9:12
You're looking for
.filter()
.– Madara Uchiha♦
Apr 3 at 9:09
You're looking for
.filter()
.– Madara Uchiha♦
Apr 3 at 9:09
4
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
Apr 3 at 9:12
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
Apr 3 at 9:12
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) => ;
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 Apr 3 at 10:26
answered Apr 3 at 9:08
Sebastian KaczmarekSebastian Kaczmarek
1,8071025
1,8071025
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) => ;
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) => ;
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) => ;
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) => ;
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) => ;
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) => ;
console.log(getObject("c@c.com", array));
answered Apr 3 at 9:10
EddieEddie
20.4k51642
20.4k51642
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 Apr 3 at 9:09
Nina ScholzNina Scholz
199k15112182
199k15112182
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 Apr 3 at 12:32
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 Apr 3 at 11:02
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♦
Apr 3 at 9:09
4
Possible duplicate of Javascript: How to filter object array based on attributes?
– Shibon
Apr 3 at 9:12