Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

javascript - Underscore js find item by 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.

here's the data:

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

Any idea how to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

Browser support

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

More information an polyfill on MDN


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.


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

If it's something like this :

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"
    }
]

You can use something like :

var filteredGoal = _.where(goal, {id: "1"});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...