41

我是 Underscore js 的新手,对如何使用它有点困惑。我有一组“目标”,我想通过 ID 找到其中一个。

这是数据:

{"goal":[
    {
        "category" : "education",
        "title" : "Charlie University",
        "description" : "Lorem ipsum dolor sit amet",
        "date" : "01/03/2020",
        "value" : 50000,
        "achievability" : 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [
            {
                ...
            },
            {
                ...
            }
        ],
        "articles": [
            {
                ...
            },
            {
                ...
            },
            {
                ...
            }
        ],
        "related_goals": [
            {
                ...
            }
        ],
        "id":"1"
    },
    {
        "category" : "family",
        "title" : "Getting married",
        "description" : "Lorem ipsum dolor sit amet",
        "date" : "01/03/2022",
        "value" : 10000,
        "achievability" : 3,
        "experimental_achievability": 2,
        "suggested": true,
        "accounts": [
            {
                ...
            }
        ],
        "articles": [
            {
                ...
            },
            {
                ...
            },
            {
                ...
            }
        ],
        "related_goals": [
            {
                ...
            }
        ],
        "id":"2"
    }
    ...
]}

这就是我正在尝试的,我想获取整个数组/对象,以便我可以获取它的每个字段:

var goalId = 1;
_.each(result.goal, function(item){
    _.find(result.goal, function(i){
         return i = goalId;
    });
});

知道怎么做吗?

4

3 回答 3

99

更新

现在是 2016 年,我们可能不需要下划线来实现这一目标。使用Array.prototype.find(). 如果数组中的元素满足提供的测试功能,则返回数组中的值。否则返回未定义。

  // Underscore
  var users = [
    { 'user': 'barney',  'age': 36, 'active': true },
    { 'user': 'fred',    'age': 40, 'active': false },
    { 'user': 'pebbles', 'age': 1,  'active': true }
  ]

  _.find(users, function (o) { return o.age < 40; })
  // output: object for 'barney'

  // Native
  var users = [
    { 'user': 'barney',  'age': 36, 'active': true },
    { 'user': 'fred',    'age': 40, 'active': false },
    { 'user': 'pebbles', 'age': 1,  'active': true }
  ]

  users.find(function (o) { return o.age < 40; })
  // output: object for 'barney'

浏览器支持

--------------------------------------------
| Chrome | Firefox | Safari |  IE  | Opera |
|--------|---------|--------|------|-------|
|   45   |    25   |  7.1   | Edge |  32   |
--------------------------------------------

更多信息MDN上的 polyfill


更新:我发现_.where总是返回一个数组。_.findWhere返回它找到的第一个对象,因此如果您期望返回单个对象,则使用它会更好。


你可以使用_.where它更容易。

如果是这样的:

var goal  = [

    {
        "category" : "education",
        "title" : "Charlie University",
        "description" : "Lorem ipsum dolor sit amet",
        "date" : "01/03/2020",
        "value" : 50000,
        "achievability" : 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id":"1"
    },
    {
        "category" : "education",
        "title" : "Charlie University",
        "description" : "Lorem ipsum dolor sit amet",
        "date" : "01/03/2020",
        "value" : 50000,
        "achievability" : 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id":"2"
    },
    {
        "category" : "education",
        "title" : "Charlie University",
        "description" : "Lorem ipsum dolor sit amet",
        "date" : "01/03/2020",
        "value" : 50000,
        "achievability" : 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id":"3"
    },
    {
        "category" : "education",
        "title" : "Charlie University",
        "description" : "Lorem ipsum dolor sit amet",
        "date" : "01/03/2020",
        "value" : 50000,
        "achievability" : 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id":"4"
    }
]

你可以使用类似的东西:

var filteredGoal = _.where(goal, {id: "1"});
于 2012-10-05T10:49:28.350 回答
21

您正在使用对象数组。所以,你可以使用:_。findWhere(查看列表并返回匹配所有键值对的第一个值)以获取基于 id 或其他键属性的所有属性。

var some= [
             {Employee:'ved',id:20}, 
             {Employee:"ved",age:25},
             {Employee:"p",age:2}
          ];

var a = _.findWhere(some,{id:20});
console.log('searchResult',a);

要获取索引,您可以使用以下内容:

var b = _.indexOf(some,a);
console.log('index',b);

如果您需要所有使用列表
请尝试 _. where(它查看数组中的每个匹配项,返回包含属性中列出的键值对的所有值的数组。)

var some= [ 
            {Employee:"ved",id:20}, 
            {Employee:"ved prakash",id:20},
            {Employee:"anyone",id:2}
          ];
var a = _.where(some,{id:25});
    console.log('searchResult',a);

_.find:它只用于检查值,而不是同时检查键值。


访问文档: _. find

于 2014-08-21T07:18:07.613 回答
15

已经简化了您的数据模型,但是像这样?

var goals = [{id:1, name:'Goal1'},
             {id:2, name:'Goal2'},
             {id:3, name:'Goal3'}];

function getGoal(id) {
    return _.find(goals, function(goal) {
        return goal.id === id;
    });
}

alert(getGoal(2).name);

您可以在这个jsFiddle中看到这一点。

于 2012-10-05T10:47:46.220 回答