Get the index of the first element in an array with value greater than x
I have this array:
var array = [400, 4000, 400, 400, 4000];
How can I get the index of the first element with value greater than 400?
Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.
If there are duplicates of my question that apply to JS, I'd really want to see them.
javascript arrays
add a comment |
I have this array:
var array = [400, 4000, 400, 400, 4000];
How can I get the index of the first element with value greater than 400?
Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.
If there are duplicates of my question that apply to JS, I'd really want to see them.
javascript arrays
add a comment |
I have this array:
var array = [400, 4000, 400, 400, 4000];
How can I get the index of the first element with value greater than 400?
Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.
If there are duplicates of my question that apply to JS, I'd really want to see them.
javascript arrays
I have this array:
var array = [400, 4000, 400, 400, 4000];
How can I get the index of the first element with value greater than 400?
Note: While making sure this question was unique, I came across questions asking this same problem- but for different programming languages.
If there are duplicates of my question that apply to JS, I'd really want to see them.
javascript arrays
javascript arrays
asked Jan 12 '17 at 0:10
someGuysomeGuy
325
325
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use findIndex here
check this snippet
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number)
return number > 400;
);
console.log(index);
3
or in ES6var index = array.findIndex(n => n > 400);
– Alnitak
Jan 12 '17 at 0:19
@RobG so it is - I misread the MDN page as saying it was in ES5
– Alnitak
Jan 12 '17 at 0:25
1
It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)
– RobG
Jan 12 '17 at 0:27
add a comment |
You can use a simple for
loop and check each element.
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i<l; i++)
if(array[i] > 400)
result = i;
break;
if(typeof result !== 'undefined')
console.log('number greater than 400 found at array index: ' + result);
else
console.log('no number greater than 400 found in the given arrry.');
Read up: for
- JavaScript | MDN
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%2f41603036%2fget-the-index-of-the-first-element-in-an-array-with-value-greater-than-x%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use findIndex here
check this snippet
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number)
return number > 400;
);
console.log(index);
3
or in ES6var index = array.findIndex(n => n > 400);
– Alnitak
Jan 12 '17 at 0:19
@RobG so it is - I misread the MDN page as saying it was in ES5
– Alnitak
Jan 12 '17 at 0:25
1
It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)
– RobG
Jan 12 '17 at 0:27
add a comment |
You can use findIndex here
check this snippet
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number)
return number > 400;
);
console.log(index);
3
or in ES6var index = array.findIndex(n => n > 400);
– Alnitak
Jan 12 '17 at 0:19
@RobG so it is - I misread the MDN page as saying it was in ES5
– Alnitak
Jan 12 '17 at 0:25
1
It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)
– RobG
Jan 12 '17 at 0:27
add a comment |
You can use findIndex here
check this snippet
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number)
return number > 400;
);
console.log(index);
You can use findIndex here
check this snippet
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number)
return number > 400;
);
console.log(index);
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number)
return number > 400;
);
console.log(index);
var array = [400, 4000, 400, 400, 4000];
var index=array.findIndex(function(number)
return number > 400;
);
console.log(index);
answered Jan 12 '17 at 0:17
GeekyGeeky
6,00411237
6,00411237
3
or in ES6var index = array.findIndex(n => n > 400);
– Alnitak
Jan 12 '17 at 0:19
@RobG so it is - I misread the MDN page as saying it was in ES5
– Alnitak
Jan 12 '17 at 0:25
1
It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)
– RobG
Jan 12 '17 at 0:27
add a comment |
3
or in ES6var index = array.findIndex(n => n > 400);
– Alnitak
Jan 12 '17 at 0:19
@RobG so it is - I misread the MDN page as saying it was in ES5
– Alnitak
Jan 12 '17 at 0:25
1
It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)
– RobG
Jan 12 '17 at 0:27
3
3
or in ES6
var index = array.findIndex(n => n > 400);
– Alnitak
Jan 12 '17 at 0:19
or in ES6
var index = array.findIndex(n => n > 400);
– Alnitak
Jan 12 '17 at 0:19
@RobG so it is - I misread the MDN page as saying it was in ES5
– Alnitak
Jan 12 '17 at 0:25
@RobG so it is - I misread the MDN page as saying it was in ES5
– Alnitak
Jan 12 '17 at 0:25
1
1
It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)
– RobG
Jan 12 '17 at 0:27
It's a little confusing that the edition numbers are one off the year number. Perhaps they should skip a year so they align. ;-)
– RobG
Jan 12 '17 at 0:27
add a comment |
You can use a simple for
loop and check each element.
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i<l; i++)
if(array[i] > 400)
result = i;
break;
if(typeof result !== 'undefined')
console.log('number greater than 400 found at array index: ' + result);
else
console.log('no number greater than 400 found in the given arrry.');
Read up: for
- JavaScript | MDN
add a comment |
You can use a simple for
loop and check each element.
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i<l; i++)
if(array[i] > 400)
result = i;
break;
if(typeof result !== 'undefined')
console.log('number greater than 400 found at array index: ' + result);
else
console.log('no number greater than 400 found in the given arrry.');
Read up: for
- JavaScript | MDN
add a comment |
You can use a simple for
loop and check each element.
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i<l; i++)
if(array[i] > 400)
result = i;
break;
if(typeof result !== 'undefined')
console.log('number greater than 400 found at array index: ' + result);
else
console.log('no number greater than 400 found in the given arrry.');
Read up: for
- JavaScript | MDN
You can use a simple for
loop and check each element.
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i<l; i++)
if(array[i] > 400)
result = i;
break;
if(typeof result !== 'undefined')
console.log('number greater than 400 found at array index: ' + result);
else
console.log('no number greater than 400 found in the given arrry.');
Read up: for
- JavaScript | MDN
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i<l; i++)
if(array[i] > 400)
result = i;
break;
if(typeof result !== 'undefined')
console.log('number greater than 400 found at array index: ' + result);
else
console.log('no number greater than 400 found in the given arrry.');
var array = [400, 4000, 400, 400, 4000];
var result;
for(var i=0, l=array.length; i<l; i++)
if(array[i] > 400)
result = i;
break;
if(typeof result !== 'undefined')
console.log('number greater than 400 found at array index: ' + result);
else
console.log('no number greater than 400 found in the given arrry.');
edited Nov 15 '18 at 18:22
answered Jan 12 '17 at 0:14
Rahul DesaiRahul Desai
11k1064106
11k1064106
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%2f41603036%2fget-the-index-of-the-first-element-in-an-array-with-value-greater-than-x%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