Exit main function from inside forEach loop inside nested function [duplicate]Breaking out of an inner foreach loopWhat's the best way to break from nested loops in JavaScript?JavaScript closure inside loops – simple practical exampleHow to break/exit from a each() function in JQuery?Early exit from function?.getJSON request never exitsLoop inside React JSXJquery foreach functionUsing async/await with a forEach loopHow to return an element in a forEach loopAccessing object value inside array using forEach?
I know that there is a preselected candidate for a position to be filled at my department. What should I do?
Must a warlock replace spells with new spells of exactly their Pact Magic spell slot level?
How do I get the ς (final sigma) symbol?
USPS Back Room - Trespassing?
Which European Languages are not Indo-European?
Security vulnerabilities of POST over SSL
WordPress 5.2.1 deactivated my jQuery
Why is the Eisenstein ideal paper so great?
Time complexity of an algorithm: Is it important to state the base of the logarithm?
What is the use case for non-breathable waterproof pants?
便利な工具 what does な means
Is it legal to meet with potential future employers in the UK, whilst visiting from the USA
The art of clickbait captions
Making a electromagnet
How to patch glass cuts in a bicycle tire?
My players want to grind XP but we're using milestone advancement
Should there be an "a" before "ten years imprisonment"?
If a (distance) metric on a connected Riemannian manifold locally agrees with the Riemannian metric, is it equal to the induced metric?
Why did the person in charge of a principality not just declare themself king?
Beginner looking to learn/master musical theory and instrumental ability. Where should I begin?
How to melt snow without fire or body heat?
How did NASA Langley end up with the first 737?
Why did Jon Snow do this immoral act if he is so honorable?
Drums and punctuation
Exit main function from inside forEach loop inside nested function [duplicate]
Breaking out of an inner foreach loopWhat's the best way to break from nested loops in JavaScript?JavaScript closure inside loops – simple practical exampleHow to break/exit from a each() function in JQuery?Early exit from function?.getJSON request never exitsLoop inside React JSXJquery foreach functionUsing async/await with a forEach loopHow to return an element in a forEach loopAccessing object value inside array using forEach?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Breaking out of an inner foreach loop
2 answers
I have a JSON object that contains an array of objects. I also have an array of desired values, and I want to search for these values inside my JSON. I only care about the first match. If no match is found, throw an error.
There might be a better way to do this but this is what I came up with:
function myFunction()
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
db.arrayOfObjects.forEach(object =>
if (object.propertyValue === desiredValues[i])
console.log("Match found!");
return; // break out of myFunction()
);
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
My problem is that the return statement only breaks out of the current iteration of forEach (why?). The remaining objects are still tested for all remaining values of desiredValues, and the error is always thrown. How can I exit myFunction() entirely when a match is found, or how can I restructure this function to achieve what I want?
Edit: I probably should have mentioned I also need to do stuff with the object that was matched, so not just return true if there is a match.
javascript jquery
marked as duplicate by meagar♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 1:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Breaking out of an inner foreach loop
2 answers
I have a JSON object that contains an array of objects. I also have an array of desired values, and I want to search for these values inside my JSON. I only care about the first match. If no match is found, throw an error.
There might be a better way to do this but this is what I came up with:
function myFunction()
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
db.arrayOfObjects.forEach(object =>
if (object.propertyValue === desiredValues[i])
console.log("Match found!");
return; // break out of myFunction()
);
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
My problem is that the return statement only breaks out of the current iteration of forEach (why?). The remaining objects are still tested for all remaining values of desiredValues, and the error is always thrown. How can I exit myFunction() entirely when a match is found, or how can I restructure this function to achieve what I want?
Edit: I probably should have mentioned I also need to do stuff with the object that was matched, so not just return true if there is a match.
javascript jquery
marked as duplicate by meagar♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 1:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Breaking out of an inner foreach loop
2 answers
I have a JSON object that contains an array of objects. I also have an array of desired values, and I want to search for these values inside my JSON. I only care about the first match. If no match is found, throw an error.
There might be a better way to do this but this is what I came up with:
function myFunction()
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
db.arrayOfObjects.forEach(object =>
if (object.propertyValue === desiredValues[i])
console.log("Match found!");
return; // break out of myFunction()
);
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
My problem is that the return statement only breaks out of the current iteration of forEach (why?). The remaining objects are still tested for all remaining values of desiredValues, and the error is always thrown. How can I exit myFunction() entirely when a match is found, or how can I restructure this function to achieve what I want?
Edit: I probably should have mentioned I also need to do stuff with the object that was matched, so not just return true if there is a match.
javascript jquery
This question already has an answer here:
Breaking out of an inner foreach loop
2 answers
I have a JSON object that contains an array of objects. I also have an array of desired values, and I want to search for these values inside my JSON. I only care about the first match. If no match is found, throw an error.
There might be a better way to do this but this is what I came up with:
function myFunction()
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
db.arrayOfObjects.forEach(object =>
if (object.propertyValue === desiredValues[i])
console.log("Match found!");
return; // break out of myFunction()
);
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
My problem is that the return statement only breaks out of the current iteration of forEach (why?). The remaining objects are still tested for all remaining values of desiredValues, and the error is always thrown. How can I exit myFunction() entirely when a match is found, or how can I restructure this function to achieve what I want?
Edit: I probably should have mentioned I also need to do stuff with the object that was matched, so not just return true if there is a match.
This question already has an answer here:
Breaking out of an inner foreach loop
2 answers
javascript jquery
javascript jquery
edited Mar 24 at 2:00
Matheus Leão
asked Mar 24 at 1:16
Matheus LeãoMatheus Leão
617
617
marked as duplicate by meagar♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 1:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by meagar♦
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 24 at 1:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Simply use .find
on array.
function myFunction() {
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
if (
db.arrayOfObjects
.find(object => object.propertyValue === desiredValues[i])
!== undefined
)
console.log("Found match! Matched value:", desiredValues[i]);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or map array of objects and mix .find
with .includes
:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const match = values.find(value => desiredValues.includes(value));
if (match !== undefined)
console.log("Found match! Matched value:", match);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or .filter
it to get all matched values:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const matches = values.filter(value => desiredValues.includes(value));
if (matches.length)
console.log("Found", matches.length, "matches! Matched values:", matches);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
In the second example, if there is a match to a value of zero, won't the function still throw an error? This seems to do exactly what I need, except that if statement might need to be a little more specific.
– Matheus Leão
Mar 24 at 2:31
@MatheusLeão thank You, I forgot to check it when was trying to minimize code and make it more readable
– num8er
Mar 24 at 10:35
add a comment |
The problem with return
inside forEach
is that it terminates the current forEach callback only - that's what return
does, it stops the current execution of a function and (possibly) returns a value, without affecting possible future calls of that function.
Use two nested Array.prototype.some
instead, which will break out of both immediately and return true
once a match is found, and iterate completely through and return false
otherwise:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
Or, if you're not comfortable with destructuring arguments:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(object => (
propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
To identify which value was found, use .find
instead for the outer loop:
const valueFound = desiredValues.find(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!valueFound)
throw new Error('Match not found');
Just to answer the minor question the OP asked for their original code, the return statement only breaks out of the current iteration of forEach becuase that return statement is inside of the callback that is invoked during every forEach iteration.
– pl61
Mar 24 at 1:30
What if I want to identify which object has the matched value? Can I just use .find in the inner loop?
– Matheus Leão
Mar 24 at 1:58
@MatheusLeão Yes - use.find
for the inner loop, and assign the result to an outer variable while still inside the outer loop.
– CertainPerformance
Mar 24 at 2:12
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simply use .find
on array.
function myFunction() {
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
if (
db.arrayOfObjects
.find(object => object.propertyValue === desiredValues[i])
!== undefined
)
console.log("Found match! Matched value:", desiredValues[i]);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or map array of objects and mix .find
with .includes
:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const match = values.find(value => desiredValues.includes(value));
if (match !== undefined)
console.log("Found match! Matched value:", match);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or .filter
it to get all matched values:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const matches = values.filter(value => desiredValues.includes(value));
if (matches.length)
console.log("Found", matches.length, "matches! Matched values:", matches);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
In the second example, if there is a match to a value of zero, won't the function still throw an error? This seems to do exactly what I need, except that if statement might need to be a little more specific.
– Matheus Leão
Mar 24 at 2:31
@MatheusLeão thank You, I forgot to check it when was trying to minimize code and make it more readable
– num8er
Mar 24 at 10:35
add a comment |
Simply use .find
on array.
function myFunction() {
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
if (
db.arrayOfObjects
.find(object => object.propertyValue === desiredValues[i])
!== undefined
)
console.log("Found match! Matched value:", desiredValues[i]);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or map array of objects and mix .find
with .includes
:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const match = values.find(value => desiredValues.includes(value));
if (match !== undefined)
console.log("Found match! Matched value:", match);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or .filter
it to get all matched values:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const matches = values.filter(value => desiredValues.includes(value));
if (matches.length)
console.log("Found", matches.length, "matches! Matched values:", matches);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
In the second example, if there is a match to a value of zero, won't the function still throw an error? This seems to do exactly what I need, except that if statement might need to be a little more specific.
– Matheus Leão
Mar 24 at 2:31
@MatheusLeão thank You, I forgot to check it when was trying to minimize code and make it more readable
– num8er
Mar 24 at 10:35
add a comment |
Simply use .find
on array.
function myFunction() {
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
if (
db.arrayOfObjects
.find(object => object.propertyValue === desiredValues[i])
!== undefined
)
console.log("Found match! Matched value:", desiredValues[i]);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or map array of objects and mix .find
with .includes
:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const match = values.find(value => desiredValues.includes(value));
if (match !== undefined)
console.log("Found match! Matched value:", match);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or .filter
it to get all matched values:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const matches = values.filter(value => desiredValues.includes(value));
if (matches.length)
console.log("Found", matches.length, "matches! Matched values:", matches);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
Simply use .find
on array.
function myFunction() {
$.getJSON('database.json')
.done(db =>
for (let i = 0; i < desiredValues.length; i++)
if (
db.arrayOfObjects
.find(object => object.propertyValue === desiredValues[i])
!== undefined
)
console.log("Found match! Matched value:", desiredValues[i]);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or map array of objects and mix .find
with .includes
:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const match = values.find(value => desiredValues.includes(value));
if (match !== undefined)
console.log("Found match! Matched value:", match);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
or .filter
it to get all matched values:
function myFunction()
$.getJSON('database.json')
.done(db =>
const values = db.arrayOfObjects.map(object => object.propertyValue);
const matches = values.filter(value => desiredValues.includes(value));
if (matches.length)
console.log("Found", matches.length, "matches! Matched values:", matches);
return;
throw Error("Match not found.");
)
.fail(error =>
throw Error("getJSON request failed.n" + error);
)
edited Mar 24 at 10:56
answered Mar 24 at 1:22
num8ernum8er
12.2k22240
12.2k22240
In the second example, if there is a match to a value of zero, won't the function still throw an error? This seems to do exactly what I need, except that if statement might need to be a little more specific.
– Matheus Leão
Mar 24 at 2:31
@MatheusLeão thank You, I forgot to check it when was trying to minimize code and make it more readable
– num8er
Mar 24 at 10:35
add a comment |
In the second example, if there is a match to a value of zero, won't the function still throw an error? This seems to do exactly what I need, except that if statement might need to be a little more specific.
– Matheus Leão
Mar 24 at 2:31
@MatheusLeão thank You, I forgot to check it when was trying to minimize code and make it more readable
– num8er
Mar 24 at 10:35
In the second example, if there is a match to a value of zero, won't the function still throw an error? This seems to do exactly what I need, except that if statement might need to be a little more specific.
– Matheus Leão
Mar 24 at 2:31
In the second example, if there is a match to a value of zero, won't the function still throw an error? This seems to do exactly what I need, except that if statement might need to be a little more specific.
– Matheus Leão
Mar 24 at 2:31
@MatheusLeão thank You, I forgot to check it when was trying to minimize code and make it more readable
– num8er
Mar 24 at 10:35
@MatheusLeão thank You, I forgot to check it when was trying to minimize code and make it more readable
– num8er
Mar 24 at 10:35
add a comment |
The problem with return
inside forEach
is that it terminates the current forEach callback only - that's what return
does, it stops the current execution of a function and (possibly) returns a value, without affecting possible future calls of that function.
Use two nested Array.prototype.some
instead, which will break out of both immediately and return true
once a match is found, and iterate completely through and return false
otherwise:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
Or, if you're not comfortable with destructuring arguments:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(object => (
propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
To identify which value was found, use .find
instead for the outer loop:
const valueFound = desiredValues.find(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!valueFound)
throw new Error('Match not found');
Just to answer the minor question the OP asked for their original code, the return statement only breaks out of the current iteration of forEach becuase that return statement is inside of the callback that is invoked during every forEach iteration.
– pl61
Mar 24 at 1:30
What if I want to identify which object has the matched value? Can I just use .find in the inner loop?
– Matheus Leão
Mar 24 at 1:58
@MatheusLeão Yes - use.find
for the inner loop, and assign the result to an outer variable while still inside the outer loop.
– CertainPerformance
Mar 24 at 2:12
add a comment |
The problem with return
inside forEach
is that it terminates the current forEach callback only - that's what return
does, it stops the current execution of a function and (possibly) returns a value, without affecting possible future calls of that function.
Use two nested Array.prototype.some
instead, which will break out of both immediately and return true
once a match is found, and iterate completely through and return false
otherwise:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
Or, if you're not comfortable with destructuring arguments:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(object => (
propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
To identify which value was found, use .find
instead for the outer loop:
const valueFound = desiredValues.find(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!valueFound)
throw new Error('Match not found');
Just to answer the minor question the OP asked for their original code, the return statement only breaks out of the current iteration of forEach becuase that return statement is inside of the callback that is invoked during every forEach iteration.
– pl61
Mar 24 at 1:30
What if I want to identify which object has the matched value? Can I just use .find in the inner loop?
– Matheus Leão
Mar 24 at 1:58
@MatheusLeão Yes - use.find
for the inner loop, and assign the result to an outer variable while still inside the outer loop.
– CertainPerformance
Mar 24 at 2:12
add a comment |
The problem with return
inside forEach
is that it terminates the current forEach callback only - that's what return
does, it stops the current execution of a function and (possibly) returns a value, without affecting possible future calls of that function.
Use two nested Array.prototype.some
instead, which will break out of both immediately and return true
once a match is found, and iterate completely through and return false
otherwise:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
Or, if you're not comfortable with destructuring arguments:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(object => (
propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
To identify which value was found, use .find
instead for the outer loop:
const valueFound = desiredValues.find(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!valueFound)
throw new Error('Match not found');
The problem with return
inside forEach
is that it terminates the current forEach callback only - that's what return
does, it stops the current execution of a function and (possibly) returns a value, without affecting possible future calls of that function.
Use two nested Array.prototype.some
instead, which will break out of both immediately and return true
once a match is found, and iterate completely through and return false
otherwise:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
Or, if you're not comfortable with destructuring arguments:
const isFound = desiredValues.some(valueToFind => (
db.arrayOfObjcts.some(object => (
propertyValue === valueToFind
))
));
if (!isFound)
throw new Error('Match not found');
To identify which value was found, use .find
instead for the outer loop:
const valueFound = desiredValues.find(valueToFind => (
db.arrayOfObjcts.some(( propertyValue ) => (
object.propertyValue === valueToFind
))
));
if (!valueFound)
throw new Error('Match not found');
edited Mar 24 at 1:33
answered Mar 24 at 1:19
CertainPerformanceCertainPerformance
108k166999
108k166999
Just to answer the minor question the OP asked for their original code, the return statement only breaks out of the current iteration of forEach becuase that return statement is inside of the callback that is invoked during every forEach iteration.
– pl61
Mar 24 at 1:30
What if I want to identify which object has the matched value? Can I just use .find in the inner loop?
– Matheus Leão
Mar 24 at 1:58
@MatheusLeão Yes - use.find
for the inner loop, and assign the result to an outer variable while still inside the outer loop.
– CertainPerformance
Mar 24 at 2:12
add a comment |
Just to answer the minor question the OP asked for their original code, the return statement only breaks out of the current iteration of forEach becuase that return statement is inside of the callback that is invoked during every forEach iteration.
– pl61
Mar 24 at 1:30
What if I want to identify which object has the matched value? Can I just use .find in the inner loop?
– Matheus Leão
Mar 24 at 1:58
@MatheusLeão Yes - use.find
for the inner loop, and assign the result to an outer variable while still inside the outer loop.
– CertainPerformance
Mar 24 at 2:12
Just to answer the minor question the OP asked for their original code, the return statement only breaks out of the current iteration of forEach becuase that return statement is inside of the callback that is invoked during every forEach iteration.
– pl61
Mar 24 at 1:30
Just to answer the minor question the OP asked for their original code, the return statement only breaks out of the current iteration of forEach becuase that return statement is inside of the callback that is invoked during every forEach iteration.
– pl61
Mar 24 at 1:30
What if I want to identify which object has the matched value? Can I just use .find in the inner loop?
– Matheus Leão
Mar 24 at 1:58
What if I want to identify which object has the matched value? Can I just use .find in the inner loop?
– Matheus Leão
Mar 24 at 1:58
@MatheusLeão Yes - use
.find
for the inner loop, and assign the result to an outer variable while still inside the outer loop.– CertainPerformance
Mar 24 at 2:12
@MatheusLeão Yes - use
.find
for the inner loop, and assign the result to an outer variable while still inside the outer loop.– CertainPerformance
Mar 24 at 2:12
add a comment |