1

How to find user_value closest value from this array? Here is user_value variable according to this i need find out closest value from array.

var user_value = 5500;
var array = [ 

    {_id: 5809e269d60f577ae35f6add,
      coins: 1000,
      is_active: 1,
      iconId: 4 },
    {_id: 5809e269d60f577ae35f6ade,
      coins: 2000,
      is_active: 1,
      iconId: 5 },
    {_id: 5809e269d60f577ae35f6adf,
      coins: 5000,
      is_active: 1,
      iconId: 6 },
    {_id: 5809e269d60f577ae35f6ae0,
      coins: 7000,
      is_active: 1,
      iconId: 7 },
    {_id: 5809e269d60f577ae35f6ae1,
      coins: 10000,
      is_active: 1,
      iconId: 8 },
    {_id: 5809e269d60f577ae35f6ae2,
      coins: 15000,
      is_active: 1,
      iconId: 9 } ];
5
  • How do you define the closest? Commented May 31, 2017 at 8:09
  • what us closest here? where you defined it? Commented May 31, 2017 at 8:09
  • coins filed compare for closest Commented May 31, 2017 at 8:09
  • user_value and array coins field compare for closest Commented May 31, 2017 at 8:10
  • i need closest index from array Commented May 31, 2017 at 8:11

3 Answers 3

2

You could use an iterative approach by using the absolute difference for checking.

var value = 5500,
    array = [{ _id: '5809e269d60f577ae35f6add', coins: 1000, is_active: 1, iconId: 4 }, { _id: '5809e269d60f577ae35f6ade', coins: 2000, is_active: 1, iconId: 5 }, { _id: '5809e269d60f577ae35f6adf', coins: 5000, is_active: 1, iconId: 6 }, { _id: '5809e269d60f577ae35f6ae0', coins: 7000, is_active: 1, iconId: 7 }, { _id: '5809e269d60f577ae35f6ae1', coins: 10000, is_active: 1, iconId: 8 }, { _id: '5809e269d60f577ae35f6ae2', coins: 15000, is_active: 1, iconId: 9 }],
    result = array.reduce(function (r, a, i, aa) {
        return i && Math.abs(aa[r].coins - value) < Math.abs(a.coins - value) ? r : i;
    }, -1);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you define the closest as Math.abs(array[i].coins - value);, you just need to iterate the array like below:

var user_value = 5500;
var array = [
  {
    coins: 1000,
    is_active: 1,
    iconId: 4
  },
  {
    coins: 2000,
    is_active: 1,
    iconId: 5
  },
  {
    coins: 5000,
    is_active: 1,
    iconId: 6
  },
  {
    coins: 7000,
    is_active: 1,
    iconId: 7
  },
  {
    coins: 10000,
    is_active: 1,
    iconId: 8
  },
  {
    coins: 15000,
    is_active: 1,
    iconId: 9
  }
];

function findClosest (value) {
  // By default that will be a big number
  var closestValue = Infinity;
  // We will store the index of the element
  var closestIndex = -1;
  for (var i = 0; i < array.length; ++i) {
    var diff = Math.abs(array[i].coins - value);
    if (diff < closestValue) {
      closestValue = diff;
      closestIndex = i;
    }
  }
  return closestIndex;
}

console.log("The closest index: " + findClosest(user_value));

Comments

0
var _ = require('underscore');

var user_value = 5500;
var array = [ 

    { _id: '5809e269d60f577ae35f6add',
      coins: 1000,
      is_active: 1,
      iconId: 4 },
    { _id: '5809e269d60f577ae35f6ade',
      coins: 2000,
      is_active: 1,
      iconId: 5 },
    { _id: '5809e269d60f577ae35f6adf',
      coins: 5000,
      is_active: 1,
      iconId: 6 },
    { _id: '5809e269d60f577ae35f6ae0',
      coins: 7000,
      is_active: 1,
      iconId: 7 },
    { _id: '5809e269d60f577ae35f6ae1',
      coins: 10000,
      is_active: 1,
      iconId: 8 },
    { _id: '5809e269d60f577ae35f6ae2',
      coins: 15000,
      is_active: 1,
      iconId: 9 } ];
function getClosest(array, target) {
    var tuples = _.map(array, function(json) {
        return [json, Math.abs(json.coins - target)];
    });
    //console.log(tuples);
    return _.reduce(tuples, function(memo, val) {
        return (memo[1] < val[1]) ? memo : val;
    }, [-1, 999])[0];
}

console.log(getClosest(array, user_value))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.