Underscore.js findWhere in deep JSON [英] Underscore.js findWhere in deep JSON

查看:23
本文介绍了Underscore.js findWhere in deep JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将过滤器缩小到我需要的 JSON 中的特定对象.

I'm having trouble narrowing my filter down to the specific object I need in my JSON.

JSON:

var items = {
    "itemsGroup": [
        {
            "item": [
                {"description": "Name", "value": "Jane Doe"},
                {"description": "Age", "value": 23},
                {"description": "Phone", "value": "515-555-1234"},
                {"description": "Address", "value": "123 Fake St"},
                {"description": "City", "value": "Winchestertonfieldville"},
                {"description": "State", "value": "IA"},
                {"description": "Zip", "value": 50000}
            ],
        },
        {
            "item": [
                {"description": "Eye Color", "value": "Blue"},
                {"description": "Hair", "value": "Blonde"},
                {"description": "Height", "value": "5'6"},
                {"description": "Weight", "value": 125}
            ]
        }
    ]
}

我想要在第一个嵌套数组中包含 Name 的对象:

I want the object containing Name in the first nested array:

{description":Name",value":Jane Doe"}

我可以这样实现:

_.findWhere(items.itemsGroup[0].item, {description:'Name'})

但我想考虑 JSON 可能更改的情况.

But I want to account for the case where the JSON could change.

我得到的最接近的是这个下划线函数:

The closest I've gotten is with this underscore function:

_.filter(items.itemsGroup, function(i) {
    return _.findWhere(i.item, {description:'Name'});
});

但这会返回整个第一个嵌套数组,而不是我要查找的对象(如我的第一个示例).

But that returns the entire first nested array rather than the object I'm looking for (as in my first example).

"item": [
    {"description": "Name", "value": "Jane Doe"},
    {"description": "Age", "value": 23},
    {"description": "Phone", "value": "515-555-1234"},
    {"description": "Address", "value": "123 Fake St"},
    {"description": "City", "value": "Winchestertonfieldville"},
    {"description": "State", "value": "IA"},
    {"description": "Zip", "value": 50000}
]

接下来我可以尝试什么?

What can I try next?

推荐答案

您感兴趣的所有信息都在 items.itemsGroup 中,因此我们从简单地提取该属性开始.我们把它放在一个链中,这样我们就可以一步一步地将数据按摩成我们想要的形状.

All the information you're interested in is in items.itemsGroup, so we start by simply extracting that property. We put this in a chain, so we can massage the data into the shape we want step by step.

_.chain(items.itemsGroup).value();
// [ { item: [ ... ] },
//   { item: [ ... ] } ]

这是一个对象数组.在每个对象中,我们只对 item 属性感兴趣,因此我们下一步可能只是取出该属性.

This is an array of objects. In each object, we're only interest in the item property, so our next step could be to take out just that property.

_.chain(items.itemsGroup).map('item').value();
// [ [ { description: 'Name', value: 'Jane Doe' },
//     { description: 'Age', value: 23 },
//     { description: 'Phone', value: '515-555-1234' },
//     { description: 'Address', value: '123 Fake St' },
//     { description: 'City', value: 'Winchestertonfieldville' },
//     { description: 'State', value: 'IA' },
//     { description: 'Zip', value: 50000 } ],
//   [ { description: 'Eye Color', value: 'Blue' },
//     { description: 'Hair', value: 'Blonde' },
//     { description: 'Height', value: '5\'6' },
//     { description: 'Weight', value: 125 } ] ]

现在我们有一个数组数组,最里面的数组中的每个元素都是一个 { description, value } 对.由于我们只想要第一个具有 { description: 'Name' } 的这样的对,数组的嵌套对我们来说实际上并不重要.我们可以去掉中间层的嵌套来简化我们的生活.

Now we have an array of arrays, with each element in the innermost arrays being a { description, value } pair. Since we simply want the first such pair that has { description: 'Name' }, the nesting of the arrays does not actually matter to us. We can remove the intermediate level of nesting to simplify our lives.

_.chain(items.itemsGroup).map('item').flatten().value();
// [ { description: 'Name', value: 'Jane Doe' },
//   { description: 'Age', value: 23 },
//   { description: 'Phone', value: '515-555-1234' },
//   { description: 'Address', value: '123 Fake St' },
//   { description: 'City', value: 'Winchestertonfieldville' },
//   { description: 'State', value: 'IA' },
//   { description: 'Zip', value: 50000 },
//   { description: 'Eye Color', value: 'Blue' },
//   { description: 'Hair', value: 'Blonde' },
//   { description: 'Height', value: '5\'6' },
//   { description: 'Weight', value: 125 } ]

现在只需取出第一个匹配的 { description, value } 对.

Now it's just a matter of taking out that first matching { description, value } pair.

_.chain(items.itemsGroup).map('item').flatten()
 .findWhere({ description: 'Name' }).value();
// { description: 'Name', value: 'Jane Doe' }

有关使用 Underscore 函数分解逻辑的更全面介绍,请参阅此答案.

For a more thorough introduction to decomposing logic with Underscore functions, see this answer.

这篇关于Underscore.js findWhere in deep JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆