te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - JS find() not working on array of objects - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS find() not working on array of objects - Stack Overflow

programmeradmin3浏览0评论

I have an array, clients, which I want to run array.find() on. This array contains objects, and usually looks something like this:

[ { customId: 'user1', clientId: 'TPGMNrnGtpRYtxxIAAAC' },
  { customId: 'user2', clientId: 'G80kFbp9ggAcLiDjAAAE' } ]

This is where I encounter a problem. I am trying to use find() to see if any object (or part of an object) in the array matches a certain variable, recipient, which usually contains a value like user1. the code I am using to do this is:

function checkID(recipient) {
       return recipient;
        }
    var found = clients.find(checkID);

This always returns the first object in the array. Am I using find() wrong, or is there a better way to do this?

I have an array, clients, which I want to run array.find() on. This array contains objects, and usually looks something like this:

[ { customId: 'user1', clientId: 'TPGMNrnGtpRYtxxIAAAC' },
  { customId: 'user2', clientId: 'G80kFbp9ggAcLiDjAAAE' } ]

This is where I encounter a problem. I am trying to use find() to see if any object (or part of an object) in the array matches a certain variable, recipient, which usually contains a value like user1. the code I am using to do this is:

function checkID(recipient) {
       return recipient;
        }
    var found = clients.find(checkID);

This always returns the first object in the array. Am I using find() wrong, or is there a better way to do this?

Share Improve this question edited Oct 9, 2019 at 20:26 TheDeveloperNextDoor asked Oct 9, 2019 at 20:19 TheDeveloperNextDoorTheDeveloperNextDoor 1952 gold badges2 silver badges13 bronze badges 3
  • recipient refers to the object, so return recipient will always result in a true result, meaning the first iteration through find() will return true and stop. What are you trying to check? Maybe you want something more like return recipient.clientId === clientIdVariable, as an example. – Tyler Roper Commented Oct 9, 2019 at 20:23
  • @TylerRoper Sorry, I accidentally posted the question before I was done with it. :/ – TheDeveloperNextDoor Commented Oct 9, 2019 at 20:24
  • find takes a function that receives an element from the array and returns a boolean indicating whether the element matches a condition. Your checkId function always returns the element it receives, which gets cast to a boolean, which will be true as long as the element itself isn’t falsy (false, null, 0, undefined), so it’s no surprise that this always returns the first element. – ray Commented Oct 9, 2019 at 20:27
Add a ment  | 

3 Answers 3

Reset to default 7

find takes a predicate (a function that returns true if item is a match and false if item is not a match).

const arr = [ { customId: 'user1', clientId: 'TPGMNrnGtpRYtxxIAAAC' },
              { customId: 'user2', clientId: 'G80kFbp9ggAcLiDjAAAE' } ]

const result = arr.find(item => item.customId === 'user1')
                             // ^^^^^^^^^^^^^^^^^^^^^^^^^ 
                             // This should evaluate to true for a match and to false for non-match

The reason you're getting the first item of your array all the time, is because your checkId function is returning something which evaluates to true. So, the first item is evaluated and produces a truthy result, and therefore it gets picked as the first match.


If unfamiliar with the lambda syntax () => {}, then that line is similar to:

const result = arr.find(function (item) { return item.customId === 'user1' })

You are using find wrong. If recipient contains information about the target value you should name the first param of checkID with a different name. And pare any property of it with recipient.

var found = clients.find(function(element) { return element.prop1 === recipient.anyProp;  });

To check the objects in the array for the presence of a certain customId, put the value you're searching for in an object, and pass that object to find():

let clients = [{
    customId: "user1",
    clientId: "TPGMNrnGtpRYtxxIAAAC"
  },
  {
    customId: "user2",
    clientId: "G80kFbp9ggAcLiDjAAAE"
  }
];

function checkID(el){
  return el.customId === this.param;
}

let found = clients.find(checkID, {param: "user1"});
console.info(found);

发布评论

评论列表(0)

  1. 暂无评论