在对象中找到给定数字以上和以下的元素

时间:2019-04-12 11:27:33

标签: javascript

我有如下对象

[
  {
    "value": 14,
    "name": "vwap"
  },
  {
    "value": 1,
    "name": "yopen"
  },
  {
    "value": 12,
    "name": "open"
  },
  {
    "value": 13,
    "name": "s3"
  },
  {
    "value": 9,
    "name": "fr1"
  },
  {
    "value": 10,
    "name": "fr2"
  }
]

如果输入为9,则需要输出为1,9 and 10,12,13 如果我的输入为13,则需要输出1,9,10,12,13 and 14

输出应为2个独立的对象,例如{ "value": 10, "name": "fr2" },还应对输出进行排序。

我尝试了类似下面的方法,但是它仅适用于数组。

function getVal(array, val, dir) {
  for (var i=0; i < array.length; i++) {
    if (dir == true) {
      if (array[i] > val){
        return array[i-1] || 0;
      }
    } else {
      if (array[i] >= val) {
        return array[i];
      }
    }
  }
}

5 个答案:

答案 0 :(得分:2)

您可以使用filter()并检查给定数字是否小于或大于对象值,并在末尾使用sort()

const arr = [ { "value": 14, "name": "vwap" }, { "value": 1, "name": "yopen" }, { "value": 12, "name": "open" }, { "value": 13, "name": "s3" }, { "value": 9, "name": "fr1" }, { "value": 10, "name": "fr2" } ]

function getParts(arr,num,min=0,max=Infinity){
  let first = arr.filter(x => num >= x.value && x.value > min && x.value < max).sort((a,b) => a.value-b.value);
  let second = arr.filter(x => num < x.value && x.value < max && x.value > min).sort((a,b) => a.value-b.value);
  return [first,second];
}

console.log(getParts(arr,9,5,12))
console.log('----------For 13--------------')
console.log(getParts(arr,13))

另一种方法是先sort()数组,然后slice()数组。

const arr = [ { "value": 14, "name": "vwap" }, { "value": 1, "name": "yopen" }, { "value": 12, "name": "open" }, { "value": 13, "name": "s3" }, { "value": 9, "name": "fr1" }, { "value": 10, "name": "fr2" } ]

function getParts(arr,num){
  let temp = arr.slice().sort((a,b) => a.value - b.value);
  let index = temp.findIndex(x => x.value === num);
  return [temp.slice(0,index+1),temp.slice(index)];
}

console.log(JSON.parse(JSON.stringify(getParts(arr,9))))
console.log('----------For 13--------------')
console.log(JSON.parse(JSON.stringify(getParts(arr,13))))

答案 1 :(得分:1)

您可以检查一下并将对象推入所需的数组中。

function getParts(value) {
    return data.reduce((r, o) => (r[+(o.value > value)].push(o), r), [[], []]);
}

var data = [{ value: 14, name: "vwap" }, { value: 1, name: "yopen" }, { value: 12, name: "open" }, { value: 13, name: "s3" }, { value: 9, name: "fr1" }, { value: 10, name: "fr2" }];

data.sort(({ value: a }, { value: b }) => a - b);
console.log(getParts(9));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)

您可以使用一个对象来存储您的结果,同时包含下部和上部。

然后,循环数组并将值与输入进行比较。您会知道元素的下部或上部存储在何处

let datas = [{"value":14,"name":"vwap"},{"value":1,"name":"yopen"},{"value":12,"name":"open"},{"value":13,"name":"s3"},{"value":9,"name":"fr1"},{"value":10,"name":"fr2"}];

function getVal(input)
{
  let result =
  {
    lowerPart: [],
    upperPart: []
  };
  
  datas.forEach((elem) =>
  {
    if (elem.value <= input)
      result.lowerPart.push(elem);
    else
      result.upperPart.push(elem);
  });
  
  return result;
}

console.log(getVal(9));

console.log(getVal(13));

答案 3 :(得分:1)

使用reduce()

var arr = [{"value":14,"name":"vwap"},{"value":1,"name":"yopen"},{"value":12,"name":"open"},{"value":13,"name":"s3"},{"value":9,"name":"fr1"},{"value":10,"name":"fr2"}]

function getVal(arr, find) {
  return arr.reduce((acc, i) => {
    acc[i.value <= find ? 'l' : 'g'].push(i)
    return acc
  }, {
    l: [],
    g: []
  })
}

console.log(getVal(arr, 9))

console.log(getVal(arr, 13))
.as-console-wrapper { max-height: 100% !important; top: 0; }

用法

let res = getVal(arr, 9)
res.l // lowerpart
res.g // greaterpart

答案 4 :(得分:0)

您可以根据需要使用过滤器和排序功能。

var find = 9;
var left = arr.filter(c=>c.value <= find).sort((a,b) => a.value-b.value);
var right = arr.filter(c=>c.value > find).sort((a,b) => a.value-b.value);

var arr = [
  {
    "value": 14,
    "name": "vwap"
  },
  {
    "value": 1,
    "name": "yopen"
  },
  {
    "value": 12,
    "name": "open"
  },
  {
    "value": 13,
    "name": "s3"
  },
  {
    "value": 9,
    "name": "fr1"
  },
  {
    "value": 10,
    "name": "fr2"
  }
]
var find = 9;
var left = arr.filter(c=>c.value <= find).sort((a,b) => a.value-b.value);
var right = arr.filter(c=>c.value > find).sort((a,b) => a.value-b.value);
console.log('Less than or equal: ' + find);
console.log(left)
console.log('Greater than: ' + find);
console.log(right)