返回通过两个条件的对象的索引

时间:2013-10-20 22:49:20

标签: coffeescript underscore.js

我正在使用下划线js,我有一个对象数组。

[{"a":"j1","b":1,"c":1,"life":1},{"a":"j2","b":5,"c":7,"life":5},{"a":"j3","b":9,"c":0,"life":9},{"a":"j4","b":4,"c":1,"life":4}]

我想获取bc的属性值最小的对象的索引。我只想获得每个对象中的一个,以防对象共享相同的bc

我有这些递归函数

groupByMulti: (obj, values, context) -> 
  if (!values.length)
      return obj;
  byFirst = _.groupBy(obj, values[0], context)
  rest = values.slice(1)
  for i in byFirst
    byFirst[i] = groupByMulti(byFirst[i], rest, context)
  return byFirst

1 个答案:

答案 0 :(得分:0)

您可以使用Filter函数返回一个数组,其中包含通过提供的函数测试的项目。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter