下划线js按ID查找项目 [英] Underscore js find item by ID

查看:124
本文介绍了下划线js按ID查找项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Underscore js的新手,对如何使用它感到困惑。我有一个'目标'的集合,我想通过ID找到其中一个。

I'm new to Underscore js and bit confused on how to use it. I have a collection of 'goals' and I want to find one of it by 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"
    }
    ...
]}

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

That's what I'm trying, I want to get the entire array/object so I can get each field of it:

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

知道怎么做吗?

推荐答案

更新



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

Update

It's 2016 and we might not acutally need underscore to achieve that. Using Array.prototype.find(). It returns a value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.

  // 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

更新:我发现 _。其中始终返回一个数组。 _。findWhere 返回它找到的第一个对象,以便它如果你期望一个对象得到回报,那将会更好用。

Update: I found that _.where always returns an array. _.findWhere returns the first object it finds so it will be better to use if you expect a single object in return.

你可以使用 _。where 这样会容易得多。

You can use _.where It's much easier.

如果是这样的话:

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"});

这篇关于下划线js按ID查找项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆