Javascript 获取对象';在Lodash中不使用循环的情况下创建父对象

Javascript 获取对象';在Lodash中不使用循环的情况下创建父对象,javascript,lodash,Javascript,Lodash,lodash中是否有一种方法/功能可以获取特定宠物id的对象父对象,而无需编写循环遍历每个人/宠物的代码 例如:。.getParent(people,pets.id=>11)//返回{“type”:“Fish”,“id”:11} let people = [ { "name": "Jack", "pets": [ { "type":"Frog", "id":23 }, { "type":"Bird", "id

lodash中是否有一种方法/功能可以获取特定宠物id的对象父对象,而无需编写循环遍历每个人/宠物的代码

例如:
。.getParent(people,pets.id=>11)//返回{“type”:“Fish”,“id”:11}

let people = [
    {
        "name": "Jack",
        "pets": [
            { "type":"Frog", "id":23 },
            { "type":"Bird", "id":57 },
            { "type":"Fish", "id":11 }
        ]
    },
    {
        "name": "Dawn",
        "pets": [
            { "type":"Lion", "id":89 },
            { "type":"Duck", "id":51 }
        ]
    },
    {
        "name": "Anne"
    },
    {
        "name": "Josh",
        "pets": []
    }
]
比如说,

_.filter(
    _.flatMap(people, 'pets'),
    t => t && t.id === 11
)
比如说,

_.filter(
    _.flatMap(people, 'pets'),
    t => t && t.id === 11
)

不是为了让你大吃一惊,但洛达斯在幕后使用循环。:)@是的,我知道。我的
people
对象相当复杂(而且嵌套),我想找到一种利用lodash清理代码的方法。我已经有了一个解决方案,它使用多个
\uuu0.forEach
来查找宠物id。可能类似于:
让peopleWithPets=people.filter(person=>person.pet&&person.pet.length);让peopleWithFish=peopleWithPets.filter(person=>person.pets.filter(pet=>pet.id==11.length)
您可以使用lodash的
find
而不是
filter
,它会在找到符合条件的条目后立即退出循环?这不会让您大吃一惊,但lodash在幕后使用循环。:)@是的,我知道。我的
people
对象相当复杂(而且嵌套),我想找到一种利用lodash清理代码的方法。我已经有了一个解决方案,它使用多个
\uuu0.forEach
来查找宠物id。可能类似于:
让peopleWithPets=people.filter(person=>person.pet&&person.pet.length);让peopleWithFish=peopleWithPets.filter(person=>person.pets.filter(pet=>pet.id==11.length)
您可以使用lodash的
find
而不是
filter
,它会在找到符合条件的条目后立即退出循环?