How to find in an array from right without reversing?












4















I have an array that contains version numbers. They are sorted, but sparse. An example could look like this:



const versions = [
{ version: 1, data: { ... }},
{ version: 3, data: { ... }},
{ version: 17, data: { ... }},
{ version: 95, data: { ... }}
];


Now if I want to access version 17, things are easy:



const data = versions.find(item => item.version === 17);


But what if I want to access version 16, e.g.? From the domain point of view what I would like to get is the highest version that was released before the desired version, in this case version 3.



If we combine both queries, this basically means that I am looking for the last entry in the array that is <= the desired version. Now the problem is: How to do that efficiently.



Things would be really easy if there was a findRight function on arrays, because then I could write:



const data = version.findRight(item => item.version <= 16);


This would return version 3. If we ran the same command with 17, it would return version 17. The only problem here is: There is no findRight function in JavaScript 😔



I can think of two possible workarounds:




  • First, reverse the array, and then use the normal find function. This unfortunately becomes quite slow, as I need to run this query lots of times in a loop for different version arrays.

  • Second, implement a custom findRight function. This would work, but this sounds like solving a problem that has already been solved by someone else.


Before I go to implement my own function – is there a better (i.e. faster) approach that works with pure JavaScript and without npm modules?










share|improve this question


















  • 2





    Return an array that contains all versions smaller than 17 and then take the last item from that array.

    – Kokodoko
    Nov 15 '18 at 10:50











  • @Kokodoko Thanks for this idea, but this is less efficient than running a find, as a filter needs to process the entire array, and doesn't stop at the desired item. So yes, from a technical point of view it works, but I would like to have a more performant solution here.

    – Golo Roden
    Nov 15 '18 at 11:06
















4















I have an array that contains version numbers. They are sorted, but sparse. An example could look like this:



const versions = [
{ version: 1, data: { ... }},
{ version: 3, data: { ... }},
{ version: 17, data: { ... }},
{ version: 95, data: { ... }}
];


Now if I want to access version 17, things are easy:



const data = versions.find(item => item.version === 17);


But what if I want to access version 16, e.g.? From the domain point of view what I would like to get is the highest version that was released before the desired version, in this case version 3.



If we combine both queries, this basically means that I am looking for the last entry in the array that is <= the desired version. Now the problem is: How to do that efficiently.



Things would be really easy if there was a findRight function on arrays, because then I could write:



const data = version.findRight(item => item.version <= 16);


This would return version 3. If we ran the same command with 17, it would return version 17. The only problem here is: There is no findRight function in JavaScript 😔



I can think of two possible workarounds:




  • First, reverse the array, and then use the normal find function. This unfortunately becomes quite slow, as I need to run this query lots of times in a loop for different version arrays.

  • Second, implement a custom findRight function. This would work, but this sounds like solving a problem that has already been solved by someone else.


Before I go to implement my own function – is there a better (i.e. faster) approach that works with pure JavaScript and without npm modules?










share|improve this question


















  • 2





    Return an array that contains all versions smaller than 17 and then take the last item from that array.

    – Kokodoko
    Nov 15 '18 at 10:50











  • @Kokodoko Thanks for this idea, but this is less efficient than running a find, as a filter needs to process the entire array, and doesn't stop at the desired item. So yes, from a technical point of view it works, but I would like to have a more performant solution here.

    – Golo Roden
    Nov 15 '18 at 11:06














4












4








4


1






I have an array that contains version numbers. They are sorted, but sparse. An example could look like this:



const versions = [
{ version: 1, data: { ... }},
{ version: 3, data: { ... }},
{ version: 17, data: { ... }},
{ version: 95, data: { ... }}
];


Now if I want to access version 17, things are easy:



const data = versions.find(item => item.version === 17);


But what if I want to access version 16, e.g.? From the domain point of view what I would like to get is the highest version that was released before the desired version, in this case version 3.



If we combine both queries, this basically means that I am looking for the last entry in the array that is <= the desired version. Now the problem is: How to do that efficiently.



Things would be really easy if there was a findRight function on arrays, because then I could write:



const data = version.findRight(item => item.version <= 16);


This would return version 3. If we ran the same command with 17, it would return version 17. The only problem here is: There is no findRight function in JavaScript 😔



I can think of two possible workarounds:




  • First, reverse the array, and then use the normal find function. This unfortunately becomes quite slow, as I need to run this query lots of times in a loop for different version arrays.

  • Second, implement a custom findRight function. This would work, but this sounds like solving a problem that has already been solved by someone else.


Before I go to implement my own function – is there a better (i.e. faster) approach that works with pure JavaScript and without npm modules?










share|improve this question














I have an array that contains version numbers. They are sorted, but sparse. An example could look like this:



const versions = [
{ version: 1, data: { ... }},
{ version: 3, data: { ... }},
{ version: 17, data: { ... }},
{ version: 95, data: { ... }}
];


Now if I want to access version 17, things are easy:



const data = versions.find(item => item.version === 17);


But what if I want to access version 16, e.g.? From the domain point of view what I would like to get is the highest version that was released before the desired version, in this case version 3.



If we combine both queries, this basically means that I am looking for the last entry in the array that is <= the desired version. Now the problem is: How to do that efficiently.



Things would be really easy if there was a findRight function on arrays, because then I could write:



const data = version.findRight(item => item.version <= 16);


This would return version 3. If we ran the same command with 17, it would return version 17. The only problem here is: There is no findRight function in JavaScript 😔



I can think of two possible workarounds:




  • First, reverse the array, and then use the normal find function. This unfortunately becomes quite slow, as I need to run this query lots of times in a loop for different version arrays.

  • Second, implement a custom findRight function. This would work, but this sounds like solving a problem that has already been solved by someone else.


Before I go to implement my own function – is there a better (i.e. faster) approach that works with pure JavaScript and without npm modules?







javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 10:45









Golo RodenGolo Roden

59.5k59210310




59.5k59210310








  • 2





    Return an array that contains all versions smaller than 17 and then take the last item from that array.

    – Kokodoko
    Nov 15 '18 at 10:50











  • @Kokodoko Thanks for this idea, but this is less efficient than running a find, as a filter needs to process the entire array, and doesn't stop at the desired item. So yes, from a technical point of view it works, but I would like to have a more performant solution here.

    – Golo Roden
    Nov 15 '18 at 11:06














  • 2





    Return an array that contains all versions smaller than 17 and then take the last item from that array.

    – Kokodoko
    Nov 15 '18 at 10:50











  • @Kokodoko Thanks for this idea, but this is less efficient than running a find, as a filter needs to process the entire array, and doesn't stop at the desired item. So yes, from a technical point of view it works, but I would like to have a more performant solution here.

    – Golo Roden
    Nov 15 '18 at 11:06








2




2





Return an array that contains all versions smaller than 17 and then take the last item from that array.

– Kokodoko
Nov 15 '18 at 10:50





Return an array that contains all versions smaller than 17 and then take the last item from that array.

– Kokodoko
Nov 15 '18 at 10:50













@Kokodoko Thanks for this idea, but this is less efficient than running a find, as a filter needs to process the entire array, and doesn't stop at the desired item. So yes, from a technical point of view it works, but I would like to have a more performant solution here.

– Golo Roden
Nov 15 '18 at 11:06





@Kokodoko Thanks for this idea, but this is less efficient than running a find, as a filter needs to process the entire array, and doesn't stop at the desired item. So yes, from a technical point of view it works, but I would like to have a more performant solution here.

– Golo Roden
Nov 15 '18 at 11:06












8 Answers
8






active

oldest

votes


















1














You can also use "reduceRight" for this like below






const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];

function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}

console.log(getNextVersion(16))

console.log(getNextVersion(17))

console.log(getNextVersion(18))

console.log(getNextVersion(2))








share|improve this answer


























  • Thanks for this suggestion, but reduceRight always processes the entire array, and does not stop at the correct one. This could be a performance problem, unfortunately :-(

    – Golo Roden
    Nov 15 '18 at 11:08






  • 1





    No this would stop when it finds the result. That's why I have intentionally did .slice() and arr.length = 0

    – Nitish Narang
    Nov 15 '18 at 11:09






  • 1





    By doing .slice makes a copy and send it as 4th parameter in "reduce" and when we get the desired result we just return result and empty that copied array. This way entire array is not processed. At least this is what I think so. :)

    – Nitish Narang
    Nov 15 '18 at 11:12








  • 1





    Aaahhhhh! Now I got it! This is pretty clever 👍

    – Golo Roden
    Nov 15 '18 at 11:21



















1














You can use Array.findIndex() to get the index of targeted item and then use it to get before release element.




The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test







const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

var index = versions.findIndex(item => item.version === 17);
console.log(versions[index - 1])








share|improve this answer
























  • What if I want to search 16, not 17?

    – Golo Roden
    Nov 15 '18 at 11:16



















1














You can take last item from the filtered array which is nothing but the previous version:






const versions = [
{ version: 1, data: { }},
{ version: 3, data: { }},
{ version: 17, data: { }},
{ version: 95, data: { }}
];

const data = versions.filter((item,idx) => item.version < 17);
const previousV = data[data.length -1];
console.log(previousV);








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24



















1














You could create a generator, which returns the elements from the right side.






function* iterRight(array) {
var i = array.length;
while (i--) yield array[i];
}

const versions = [{ version: 1, data: {} }, { version: 3, data: {} }, { version: 17, data: {} }, { version: 95, data: {} }];

var item;

for (item of iterRight(versions)) {
if (item.version <= 16) break;
}

console.log(item);








share|improve this answer
























  • This is an interesting idea, but introducing a generator function seems to be a little overkill here. Nevertheless, great idea!

    – Golo Roden
    Nov 15 '18 at 11:23



















1














just use reduceRight()






const versions = [
{ version: 1, data: {}},
{ version: 3, data: {}},
{ version: 17, data: {}},
{ version: 95, data: {}}
];

console.log(versions.reduceRight((a,b) => a.version <= 16 ? a : b ));








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:23



















1














Try this one, hope it helps.



console.log( versions.find((item, index, arr) => item.version <= 16 && arr[index+1].version > 16) );


https://jsfiddle.net/dummy9807/o0z5xvrk/8/






share|improve this answer
























  • This is IMHO the second-best answer. It works, stops early, but its only drawback is that it processes the array from left to right (which in my case is worse than processing it from right to left, as newer versions are more often needed than older ones).

    – Golo Roden
    Nov 15 '18 at 11:24



















1














If we presume that next is true:




  1. Array is sorted

  2. We will always search for an existing item


then You could use the index of the current item:



e.g.



var data = versions.find((element, index, array) => { return array[index+1].version === 17 });


This way you will get element before the version your are looking for.






share|improve this answer
























  • Thanks for your answer! Unfortunately, assumption 2 is not given in my case :-(

    – Golo Roden
    Nov 15 '18 at 11:07











  • Then you could check if next elemet exists and if it doesn't return false before cobdidion check.

    – Senad Meškin
    Nov 15 '18 at 11:10



















0














How about using a filter and get the last element






const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

function fun(val) {
let k = versions.filter(item => {
return item.version < val
})
return k[k.length - 1].version

}

console.log(fun(17))








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317653%2fhow-to-find-in-an-array-from-right-without-reversing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























8 Answers
8






active

oldest

votes








8 Answers
8






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can also use "reduceRight" for this like below






const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];

function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}

console.log(getNextVersion(16))

console.log(getNextVersion(17))

console.log(getNextVersion(18))

console.log(getNextVersion(2))








share|improve this answer


























  • Thanks for this suggestion, but reduceRight always processes the entire array, and does not stop at the correct one. This could be a performance problem, unfortunately :-(

    – Golo Roden
    Nov 15 '18 at 11:08






  • 1





    No this would stop when it finds the result. That's why I have intentionally did .slice() and arr.length = 0

    – Nitish Narang
    Nov 15 '18 at 11:09






  • 1





    By doing .slice makes a copy and send it as 4th parameter in "reduce" and when we get the desired result we just return result and empty that copied array. This way entire array is not processed. At least this is what I think so. :)

    – Nitish Narang
    Nov 15 '18 at 11:12








  • 1





    Aaahhhhh! Now I got it! This is pretty clever 👍

    – Golo Roden
    Nov 15 '18 at 11:21
















1














You can also use "reduceRight" for this like below






const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];

function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}

console.log(getNextVersion(16))

console.log(getNextVersion(17))

console.log(getNextVersion(18))

console.log(getNextVersion(2))








share|improve this answer


























  • Thanks for this suggestion, but reduceRight always processes the entire array, and does not stop at the correct one. This could be a performance problem, unfortunately :-(

    – Golo Roden
    Nov 15 '18 at 11:08






  • 1





    No this would stop when it finds the result. That's why I have intentionally did .slice() and arr.length = 0

    – Nitish Narang
    Nov 15 '18 at 11:09






  • 1





    By doing .slice makes a copy and send it as 4th parameter in "reduce" and when we get the desired result we just return result and empty that copied array. This way entire array is not processed. At least this is what I think so. :)

    – Nitish Narang
    Nov 15 '18 at 11:12








  • 1





    Aaahhhhh! Now I got it! This is pretty clever 👍

    – Golo Roden
    Nov 15 '18 at 11:21














1












1








1







You can also use "reduceRight" for this like below






const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];

function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}

console.log(getNextVersion(16))

console.log(getNextVersion(17))

console.log(getNextVersion(18))

console.log(getNextVersion(2))








share|improve this answer















You can also use "reduceRight" for this like below






const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];

function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}

console.log(getNextVersion(16))

console.log(getNextVersion(17))

console.log(getNextVersion(18))

console.log(getNextVersion(2))








const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];

function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}

console.log(getNextVersion(16))

console.log(getNextVersion(17))

console.log(getNextVersion(18))

console.log(getNextVersion(2))





const versions = [
{ version: 1, data: 1},
{ version: 3, data: 3},
{ version: 17, data: 17},
{ version: 95, data: 95}
];

function getNextVersion(v) {
return versions.slice(0).reduceRight((a , b, i , arr) => b.version <= v ? (arr.length = 0, b) : b)
}

console.log(getNextVersion(16))

console.log(getNextVersion(17))

console.log(getNextVersion(18))

console.log(getNextVersion(2))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 16:35

























answered Nov 15 '18 at 11:06









Nitish NarangNitish Narang

2,9601815




2,9601815













  • Thanks for this suggestion, but reduceRight always processes the entire array, and does not stop at the correct one. This could be a performance problem, unfortunately :-(

    – Golo Roden
    Nov 15 '18 at 11:08






  • 1





    No this would stop when it finds the result. That's why I have intentionally did .slice() and arr.length = 0

    – Nitish Narang
    Nov 15 '18 at 11:09






  • 1





    By doing .slice makes a copy and send it as 4th parameter in "reduce" and when we get the desired result we just return result and empty that copied array. This way entire array is not processed. At least this is what I think so. :)

    – Nitish Narang
    Nov 15 '18 at 11:12








  • 1





    Aaahhhhh! Now I got it! This is pretty clever 👍

    – Golo Roden
    Nov 15 '18 at 11:21



















  • Thanks for this suggestion, but reduceRight always processes the entire array, and does not stop at the correct one. This could be a performance problem, unfortunately :-(

    – Golo Roden
    Nov 15 '18 at 11:08






  • 1





    No this would stop when it finds the result. That's why I have intentionally did .slice() and arr.length = 0

    – Nitish Narang
    Nov 15 '18 at 11:09






  • 1





    By doing .slice makes a copy and send it as 4th parameter in "reduce" and when we get the desired result we just return result and empty that copied array. This way entire array is not processed. At least this is what I think so. :)

    – Nitish Narang
    Nov 15 '18 at 11:12








  • 1





    Aaahhhhh! Now I got it! This is pretty clever 👍

    – Golo Roden
    Nov 15 '18 at 11:21

















Thanks for this suggestion, but reduceRight always processes the entire array, and does not stop at the correct one. This could be a performance problem, unfortunately :-(

– Golo Roden
Nov 15 '18 at 11:08





Thanks for this suggestion, but reduceRight always processes the entire array, and does not stop at the correct one. This could be a performance problem, unfortunately :-(

– Golo Roden
Nov 15 '18 at 11:08




1




1





No this would stop when it finds the result. That's why I have intentionally did .slice() and arr.length = 0

– Nitish Narang
Nov 15 '18 at 11:09





No this would stop when it finds the result. That's why I have intentionally did .slice() and arr.length = 0

– Nitish Narang
Nov 15 '18 at 11:09




1




1





By doing .slice makes a copy and send it as 4th parameter in "reduce" and when we get the desired result we just return result and empty that copied array. This way entire array is not processed. At least this is what I think so. :)

– Nitish Narang
Nov 15 '18 at 11:12







By doing .slice makes a copy and send it as 4th parameter in "reduce" and when we get the desired result we just return result and empty that copied array. This way entire array is not processed. At least this is what I think so. :)

– Nitish Narang
Nov 15 '18 at 11:12






1




1





Aaahhhhh! Now I got it! This is pretty clever 👍

– Golo Roden
Nov 15 '18 at 11:21





Aaahhhhh! Now I got it! This is pretty clever 👍

– Golo Roden
Nov 15 '18 at 11:21













1














You can use Array.findIndex() to get the index of targeted item and then use it to get before release element.




The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test







const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

var index = versions.findIndex(item => item.version === 17);
console.log(versions[index - 1])








share|improve this answer
























  • What if I want to search 16, not 17?

    – Golo Roden
    Nov 15 '18 at 11:16
















1














You can use Array.findIndex() to get the index of targeted item and then use it to get before release element.




The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test







const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

var index = versions.findIndex(item => item.version === 17);
console.log(versions[index - 1])








share|improve this answer
























  • What if I want to search 16, not 17?

    – Golo Roden
    Nov 15 '18 at 11:16














1












1








1







You can use Array.findIndex() to get the index of targeted item and then use it to get before release element.




The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test







const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

var index = versions.findIndex(item => item.version === 17);
console.log(versions[index - 1])








share|improve this answer













You can use Array.findIndex() to get the index of targeted item and then use it to get before release element.




The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test







const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

var index = versions.findIndex(item => item.version === 17);
console.log(versions[index - 1])








const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

var index = versions.findIndex(item => item.version === 17);
console.log(versions[index - 1])





const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

var index = versions.findIndex(item => item.version === 17);
console.log(versions[index - 1])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:51









SatpalSatpal

118k10104135




118k10104135













  • What if I want to search 16, not 17?

    – Golo Roden
    Nov 15 '18 at 11:16



















  • What if I want to search 16, not 17?

    – Golo Roden
    Nov 15 '18 at 11:16

















What if I want to search 16, not 17?

– Golo Roden
Nov 15 '18 at 11:16





What if I want to search 16, not 17?

– Golo Roden
Nov 15 '18 at 11:16











1














You can take last item from the filtered array which is nothing but the previous version:






const versions = [
{ version: 1, data: { }},
{ version: 3, data: { }},
{ version: 17, data: { }},
{ version: 95, data: { }}
];

const data = versions.filter((item,idx) => item.version < 17);
const previousV = data[data.length -1];
console.log(previousV);








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24
















1














You can take last item from the filtered array which is nothing but the previous version:






const versions = [
{ version: 1, data: { }},
{ version: 3, data: { }},
{ version: 17, data: { }},
{ version: 95, data: { }}
];

const data = versions.filter((item,idx) => item.version < 17);
const previousV = data[data.length -1];
console.log(previousV);








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24














1












1








1







You can take last item from the filtered array which is nothing but the previous version:






const versions = [
{ version: 1, data: { }},
{ version: 3, data: { }},
{ version: 17, data: { }},
{ version: 95, data: { }}
];

const data = versions.filter((item,idx) => item.version < 17);
const previousV = data[data.length -1];
console.log(previousV);








share|improve this answer













You can take last item from the filtered array which is nothing but the previous version:






const versions = [
{ version: 1, data: { }},
{ version: 3, data: { }},
{ version: 17, data: { }},
{ version: 95, data: { }}
];

const data = versions.filter((item,idx) => item.version < 17);
const previousV = data[data.length -1];
console.log(previousV);








const versions = [
{ version: 1, data: { }},
{ version: 3, data: { }},
{ version: 17, data: { }},
{ version: 95, data: { }}
];

const data = versions.filter((item,idx) => item.version < 17);
const previousV = data[data.length -1];
console.log(previousV);





const versions = [
{ version: 1, data: { }},
{ version: 3, data: { }},
{ version: 17, data: { }},
{ version: 95, data: { }}
];

const data = versions.filter((item,idx) => item.version < 17);
const previousV = data[data.length -1];
console.log(previousV);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:51









MamunMamun

28.8k71831




28.8k71831













  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24



















  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24

















In contrast to the accepted answer this always processes the entire array and doesn't stop early.

– Golo Roden
Nov 15 '18 at 11:24





In contrast to the accepted answer this always processes the entire array and doesn't stop early.

– Golo Roden
Nov 15 '18 at 11:24











1














You could create a generator, which returns the elements from the right side.






function* iterRight(array) {
var i = array.length;
while (i--) yield array[i];
}

const versions = [{ version: 1, data: {} }, { version: 3, data: {} }, { version: 17, data: {} }, { version: 95, data: {} }];

var item;

for (item of iterRight(versions)) {
if (item.version <= 16) break;
}

console.log(item);








share|improve this answer
























  • This is an interesting idea, but introducing a generator function seems to be a little overkill here. Nevertheless, great idea!

    – Golo Roden
    Nov 15 '18 at 11:23
















1














You could create a generator, which returns the elements from the right side.






function* iterRight(array) {
var i = array.length;
while (i--) yield array[i];
}

const versions = [{ version: 1, data: {} }, { version: 3, data: {} }, { version: 17, data: {} }, { version: 95, data: {} }];

var item;

for (item of iterRight(versions)) {
if (item.version <= 16) break;
}

console.log(item);








share|improve this answer
























  • This is an interesting idea, but introducing a generator function seems to be a little overkill here. Nevertheless, great idea!

    – Golo Roden
    Nov 15 '18 at 11:23














1












1








1







You could create a generator, which returns the elements from the right side.






function* iterRight(array) {
var i = array.length;
while (i--) yield array[i];
}

const versions = [{ version: 1, data: {} }, { version: 3, data: {} }, { version: 17, data: {} }, { version: 95, data: {} }];

var item;

for (item of iterRight(versions)) {
if (item.version <= 16) break;
}

console.log(item);








share|improve this answer













You could create a generator, which returns the elements from the right side.






function* iterRight(array) {
var i = array.length;
while (i--) yield array[i];
}

const versions = [{ version: 1, data: {} }, { version: 3, data: {} }, { version: 17, data: {} }, { version: 95, data: {} }];

var item;

for (item of iterRight(versions)) {
if (item.version <= 16) break;
}

console.log(item);








function* iterRight(array) {
var i = array.length;
while (i--) yield array[i];
}

const versions = [{ version: 1, data: {} }, { version: 3, data: {} }, { version: 17, data: {} }, { version: 95, data: {} }];

var item;

for (item of iterRight(versions)) {
if (item.version <= 16) break;
}

console.log(item);





function* iterRight(array) {
var i = array.length;
while (i--) yield array[i];
}

const versions = [{ version: 1, data: {} }, { version: 3, data: {} }, { version: 17, data: {} }, { version: 95, data: {} }];

var item;

for (item of iterRight(versions)) {
if (item.version <= 16) break;
}

console.log(item);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:52









Nina ScholzNina Scholz

190k1599173




190k1599173













  • This is an interesting idea, but introducing a generator function seems to be a little overkill here. Nevertheless, great idea!

    – Golo Roden
    Nov 15 '18 at 11:23



















  • This is an interesting idea, but introducing a generator function seems to be a little overkill here. Nevertheless, great idea!

    – Golo Roden
    Nov 15 '18 at 11:23

















This is an interesting idea, but introducing a generator function seems to be a little overkill here. Nevertheless, great idea!

– Golo Roden
Nov 15 '18 at 11:23





This is an interesting idea, but introducing a generator function seems to be a little overkill here. Nevertheless, great idea!

– Golo Roden
Nov 15 '18 at 11:23











1














just use reduceRight()






const versions = [
{ version: 1, data: {}},
{ version: 3, data: {}},
{ version: 17, data: {}},
{ version: 95, data: {}}
];

console.log(versions.reduceRight((a,b) => a.version <= 16 ? a : b ));








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:23
















1














just use reduceRight()






const versions = [
{ version: 1, data: {}},
{ version: 3, data: {}},
{ version: 17, data: {}},
{ version: 95, data: {}}
];

console.log(versions.reduceRight((a,b) => a.version <= 16 ? a : b ));








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:23














1












1








1







just use reduceRight()






const versions = [
{ version: 1, data: {}},
{ version: 3, data: {}},
{ version: 17, data: {}},
{ version: 95, data: {}}
];

console.log(versions.reduceRight((a,b) => a.version <= 16 ? a : b ));








share|improve this answer













just use reduceRight()






const versions = [
{ version: 1, data: {}},
{ version: 3, data: {}},
{ version: 17, data: {}},
{ version: 95, data: {}}
];

console.log(versions.reduceRight((a,b) => a.version <= 16 ? a : b ));








const versions = [
{ version: 1, data: {}},
{ version: 3, data: {}},
{ version: 17, data: {}},
{ version: 95, data: {}}
];

console.log(versions.reduceRight((a,b) => a.version <= 16 ? a : b ));





const versions = [
{ version: 1, data: {}},
{ version: 3, data: {}},
{ version: 17, data: {}},
{ version: 95, data: {}}
];

console.log(versions.reduceRight((a,b) => a.version <= 16 ? a : b ));






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:53









Artyom AmiryanArtyom Amiryan

1,9621214




1,9621214













  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:23



















  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:23

















In contrast to the accepted answer this always processes the entire array and doesn't stop early.

– Golo Roden
Nov 15 '18 at 11:23





In contrast to the accepted answer this always processes the entire array and doesn't stop early.

– Golo Roden
Nov 15 '18 at 11:23











1














Try this one, hope it helps.



console.log( versions.find((item, index, arr) => item.version <= 16 && arr[index+1].version > 16) );


https://jsfiddle.net/dummy9807/o0z5xvrk/8/






share|improve this answer
























  • This is IMHO the second-best answer. It works, stops early, but its only drawback is that it processes the array from left to right (which in my case is worse than processing it from right to left, as newer versions are more often needed than older ones).

    – Golo Roden
    Nov 15 '18 at 11:24
















1














Try this one, hope it helps.



console.log( versions.find((item, index, arr) => item.version <= 16 && arr[index+1].version > 16) );


https://jsfiddle.net/dummy9807/o0z5xvrk/8/






share|improve this answer
























  • This is IMHO the second-best answer. It works, stops early, but its only drawback is that it processes the array from left to right (which in my case is worse than processing it from right to left, as newer versions are more often needed than older ones).

    – Golo Roden
    Nov 15 '18 at 11:24














1












1








1







Try this one, hope it helps.



console.log( versions.find((item, index, arr) => item.version <= 16 && arr[index+1].version > 16) );


https://jsfiddle.net/dummy9807/o0z5xvrk/8/






share|improve this answer













Try this one, hope it helps.



console.log( versions.find((item, index, arr) => item.version <= 16 && arr[index+1].version > 16) );


https://jsfiddle.net/dummy9807/o0z5xvrk/8/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:58









Sagar Bahadur TamangSagar Bahadur Tamang

742419




742419













  • This is IMHO the second-best answer. It works, stops early, but its only drawback is that it processes the array from left to right (which in my case is worse than processing it from right to left, as newer versions are more often needed than older ones).

    – Golo Roden
    Nov 15 '18 at 11:24



















  • This is IMHO the second-best answer. It works, stops early, but its only drawback is that it processes the array from left to right (which in my case is worse than processing it from right to left, as newer versions are more often needed than older ones).

    – Golo Roden
    Nov 15 '18 at 11:24

















This is IMHO the second-best answer. It works, stops early, but its only drawback is that it processes the array from left to right (which in my case is worse than processing it from right to left, as newer versions are more often needed than older ones).

– Golo Roden
Nov 15 '18 at 11:24





This is IMHO the second-best answer. It works, stops early, but its only drawback is that it processes the array from left to right (which in my case is worse than processing it from right to left, as newer versions are more often needed than older ones).

– Golo Roden
Nov 15 '18 at 11:24











1














If we presume that next is true:




  1. Array is sorted

  2. We will always search for an existing item


then You could use the index of the current item:



e.g.



var data = versions.find((element, index, array) => { return array[index+1].version === 17 });


This way you will get element before the version your are looking for.






share|improve this answer
























  • Thanks for your answer! Unfortunately, assumption 2 is not given in my case :-(

    – Golo Roden
    Nov 15 '18 at 11:07











  • Then you could check if next elemet exists and if it doesn't return false before cobdidion check.

    – Senad Meškin
    Nov 15 '18 at 11:10
















1














If we presume that next is true:




  1. Array is sorted

  2. We will always search for an existing item


then You could use the index of the current item:



e.g.



var data = versions.find((element, index, array) => { return array[index+1].version === 17 });


This way you will get element before the version your are looking for.






share|improve this answer
























  • Thanks for your answer! Unfortunately, assumption 2 is not given in my case :-(

    – Golo Roden
    Nov 15 '18 at 11:07











  • Then you could check if next elemet exists and if it doesn't return false before cobdidion check.

    – Senad Meškin
    Nov 15 '18 at 11:10














1












1








1







If we presume that next is true:




  1. Array is sorted

  2. We will always search for an existing item


then You could use the index of the current item:



e.g.



var data = versions.find((element, index, array) => { return array[index+1].version === 17 });


This way you will get element before the version your are looking for.






share|improve this answer













If we presume that next is true:




  1. Array is sorted

  2. We will always search for an existing item


then You could use the index of the current item:



e.g.



var data = versions.find((element, index, array) => { return array[index+1].version === 17 });


This way you will get element before the version your are looking for.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:03









Senad MeškinSenad Meškin

11.1k43049




11.1k43049













  • Thanks for your answer! Unfortunately, assumption 2 is not given in my case :-(

    – Golo Roden
    Nov 15 '18 at 11:07











  • Then you could check if next elemet exists and if it doesn't return false before cobdidion check.

    – Senad Meškin
    Nov 15 '18 at 11:10



















  • Thanks for your answer! Unfortunately, assumption 2 is not given in my case :-(

    – Golo Roden
    Nov 15 '18 at 11:07











  • Then you could check if next elemet exists and if it doesn't return false before cobdidion check.

    – Senad Meškin
    Nov 15 '18 at 11:10

















Thanks for your answer! Unfortunately, assumption 2 is not given in my case :-(

– Golo Roden
Nov 15 '18 at 11:07





Thanks for your answer! Unfortunately, assumption 2 is not given in my case :-(

– Golo Roden
Nov 15 '18 at 11:07













Then you could check if next elemet exists and if it doesn't return false before cobdidion check.

– Senad Meškin
Nov 15 '18 at 11:10





Then you could check if next elemet exists and if it doesn't return false before cobdidion check.

– Senad Meškin
Nov 15 '18 at 11:10











0














How about using a filter and get the last element






const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

function fun(val) {
let k = versions.filter(item => {
return item.version < val
})
return k[k.length - 1].version

}

console.log(fun(17))








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24
















0














How about using a filter and get the last element






const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

function fun(val) {
let k = versions.filter(item => {
return item.version < val
})
return k[k.length - 1].version

}

console.log(fun(17))








share|improve this answer
























  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24














0












0








0







How about using a filter and get the last element






const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

function fun(val) {
let k = versions.filter(item => {
return item.version < val
})
return k[k.length - 1].version

}

console.log(fun(17))








share|improve this answer













How about using a filter and get the last element






const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

function fun(val) {
let k = versions.filter(item => {
return item.version < val
})
return k[k.length - 1].version

}

console.log(fun(17))








const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

function fun(val) {
let k = versions.filter(item => {
return item.version < val
})
return k[k.length - 1].version

}

console.log(fun(17))





const versions = [{
version: 1,
data: {}
},
{
version: 3,
data: {}
},
{
version: 17,
data: {}
},
{
version: 95,
data: {}
}
];

function fun(val) {
let k = versions.filter(item => {
return item.version < val
})
return k[k.length - 1].version

}

console.log(fun(17))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 10:50









brkbrk

28.3k32143




28.3k32143













  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24



















  • In contrast to the accepted answer this always processes the entire array and doesn't stop early.

    – Golo Roden
    Nov 15 '18 at 11:24

















In contrast to the accepted answer this always processes the entire array and doesn't stop early.

– Golo Roden
Nov 15 '18 at 11:24





In contrast to the accepted answer this always processes the entire array and doesn't stop early.

– Golo Roden
Nov 15 '18 at 11:24


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317653%2fhow-to-find-in-an-array-from-right-without-reversing%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values