I have a numeric value and an object containing numeric values with multiple keys. I need to find out which of the values in the object matches the value when adding.
The only solution I could find was linear:
var values={1: 10, 2: 20, 3: 30};
var value=50;
var selected=[];
$.each(values, function(k,v){
if(v==value)
selected.push(k);
$.each(values, function(k2,v2){
if(v+v2==value) {
selected.push(k);
}
});
});
console.log(selected);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Run codeHide resultThis works well for an object with two suitable values (for example, when the value is 50). But if all three are the same (set the value to 60), this will not produce any results.
Is there any way to solve this without using a recursive function call ? If so, how?
source
share