使用 _.each 查找数组中的对象

发布于 2024-12-09 13:39:09 字数 238 浏览 0 评论 0原文

我有一个看起来像这样的数组 -

     list = [{"doc":{"id": "123", "name":"abc"}}, {"doc":{"id": "345", "name":"xyz"}},{"doc":{"id": "123", "name":"str"}}]

如何使用 _.each 方法来检索 id ="123" 的 doc 对象?非常感谢任何帮助。

干杯!

I have an array which looks looks like this -

     list = [{"doc":{"id": "123", "name":"abc"}}, {"doc":{"id": "345", "name":"xyz"}},{"doc":{"id": "123", "name":"str"}}]

How can I use the _.each method to retrieve the doc object with id ="123" ? Any help is greatly appreciated.

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

趁微风不噪 2024-12-16 13:39:09

实际上, _.detect 是解决此问题的更合适的函数:

var list = [
    {"doc":{"id": "123", "name":"abc"}},
    {"doc":{"id": "345", "name":"xyz"}},
    {"doc":{"id": "123", "name":"str"}}
];

_.detect(list, function (obj) {return obj.doc.id === "123"});

result:

{"doc":{"id": "123", "name":"abc"}}

或者,如果您愿意要返回 id = '123' 的两个对象,那么您可以用 _.select 替换 _.detect。

_.select(list, function (obj) {return obj.doc.id === "123"});

结果:

[{"doc":{"id": "123", "name":"abc"}}, {"doc":{"id": "123", "name":"str"}}]

Actually, _.detect would be more a appropriate function to solve this problem:

var list = [
    {"doc":{"id": "123", "name":"abc"}},
    {"doc":{"id": "345", "name":"xyz"}},
    {"doc":{"id": "123", "name":"str"}}
];

_.detect(list, function (obj) {return obj.doc.id === "123"});

result:

{"doc":{"id": "123", "name":"abc"}}

Alternatively, if you'd like to return both objects with id = '123', then you could substitute _.detect with _.select.

_.select(list, function (obj) {return obj.doc.id === "123"});

result:

[{"doc":{"id": "123", "name":"abc"}}, {"doc":{"id": "123", "name":"str"}}]
淡忘如思 2024-12-16 13:39:09

了解 jQuery.each 如何处理中断;

var object_with_id_123;
$.each(list, function(key, val){
  if (val.doc.id == "123") {
    object_with_id_123 = val;
    return false; // break;
  }

  return true; // continue; - just to satisfy jsLint
});
console.log(object_with_id_123);

Read up on how jQuery.each handles break;

var object_with_id_123;
$.each(list, function(key, val){
  if (val.doc.id == "123") {
    object_with_id_123 = val;
    return false; // break;
  }

  return true; // continue; - just to satisfy jsLint
});
console.log(object_with_id_123);
街角迷惘 2024-12-16 13:39:09
var list = [{"doc":{"id": "123", "name":"abc"}}, {"doc":{"id": "345", "name":"xyz"}},{"doc":{"id": "123", "name":"str"}}]

var olist = [];
$.each(list,function(key, value){
    if(value.doc.id =="123"){
        olist.push(value);
    }

})

    $.each(olist,function(key, value){
                alert(JSON.stringify(value))


})
var list = [{"doc":{"id": "123", "name":"abc"}}, {"doc":{"id": "345", "name":"xyz"}},{"doc":{"id": "123", "name":"str"}}]

var olist = [];
$.each(list,function(key, value){
    if(value.doc.id =="123"){
        olist.push(value);
    }

})

    $.each(olist,function(key, value){
                alert(JSON.stringify(value))


})
梦在深巷 2024-12-16 13:39:09

因为你有 2 个 id 为 123 的结果,所以我添加了数组结果。如果您有 1 个结果,您可以返回 obj.doc 而不是将其添加到结果中

var result = [];
$.each(list, function(index, obj) { 
    if(obj.doc.id == 123) {
        result.push(obj.doc);
    }
});

because you have 2 results with id 123 i've added the array result. If you have 1 result, you could return obj.doc instead of adding it to result

var result = [];
$.each(list, function(index, obj) { 
    if(obj.doc.id == 123) {
        result.push(obj.doc);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文