最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to find a string from array of objects using underscore js? - Stack Overflow

matteradmin6PV0评论

Hi I'm trying to perform _.find using underscore function. When I tried to execute it, it returns undefined, but all my string search from this object is always there. Please find below my sample data structure:

oArrays = [
  { 
    "c":"ABC", 
    "sc":[
      { "name":"ABC123", "t1":0, "t2":0 },
      { "name":"ABC456", "t1":0, "t2":0 },
      { "name":"ABC789", "t1":0, "t2":0 }
    ] 
  },
  { 
    "c":"BCDEF", 
    "sc":[
      { "name":"JJHS", "t1":0, "t2":0 },
      { "name":"JKHJYH", "t1":0, "t2":0 },
      { "name":"DKJHKJ", "t1":0, "t2":0 }
    ] 
  },
  { 
    "c":"ZYXV", 
    "sc":[
      { "name":"KDSKD", "t1":0, "t2":0 },
      { "name":"PWIFGF", "t1":0, "t2":0 },
      { "name":"WWSD", "t1":0, "t2":0 }
    ] 
  }, 
]

_.find(oArrays, function(item){
   return item.sc.name==="ABC123" ;
});

the above codes doesn't worked, result is undefined.... Is it possible to execute it in one _.find function only?

I tried to used multiple _.map function, it works. But as much as possible I dont want to use it coz it will have too many loops. (sample below)

_.map(oArrays, function(item){
  _.find( item.sc, function( item2 ) {
    return item2.name==="ABC123" ;
  });
});

Ooops, btw I'm executing these codes using sails-controller.

really appreciate any help :)

Thanks in advance!

Hi I'm trying to perform _.find using underscore function. When I tried to execute it, it returns undefined, but all my string search from this object is always there. Please find below my sample data structure:

oArrays = [
  { 
    "c":"ABC", 
    "sc":[
      { "name":"ABC123", "t1":0, "t2":0 },
      { "name":"ABC456", "t1":0, "t2":0 },
      { "name":"ABC789", "t1":0, "t2":0 }
    ] 
  },
  { 
    "c":"BCDEF", 
    "sc":[
      { "name":"JJHS", "t1":0, "t2":0 },
      { "name":"JKHJYH", "t1":0, "t2":0 },
      { "name":"DKJHKJ", "t1":0, "t2":0 }
    ] 
  },
  { 
    "c":"ZYXV", 
    "sc":[
      { "name":"KDSKD", "t1":0, "t2":0 },
      { "name":"PWIFGF", "t1":0, "t2":0 },
      { "name":"WWSD", "t1":0, "t2":0 }
    ] 
  }, 
]

_.find(oArrays, function(item){
   return item.sc.name==="ABC123" ;
});

the above codes doesn't worked, result is undefined.... Is it possible to execute it in one _.find function only?

I tried to used multiple _.map function, it works. But as much as possible I dont want to use it coz it will have too many loops. (sample below)

_.map(oArrays, function(item){
  _.find( item.sc, function( item2 ) {
    return item2.name==="ABC123" ;
  });
});

Ooops, btw I'm executing these codes using sails-controller.

really appreciate any help :)

Thanks in advance!

Share Improve this question edited Apr 3, 2015 at 22:54 Travis Webb 15k9 gold badges59 silver badges111 bronze badges asked Sep 1, 2014 at 8:07 NinaNina 1412 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

item.sc.name is never a string, as you're trying to pare it to. In fact, it's actually undefined. But item.sc is an array of objects with the name property.

You have to check if among the name properties there's what you're looking for:

_.find(oArrays, function(item) {
    return _.contains(_.pluck(item.sc, "name"), "ABC123");
});
Post a comment

comment list (0)

  1. No comments so far