简体   繁体   English

使用Lodash使用动态键搜索JSON深度嵌套的对象?

[英]Searching JSON deeply nested Objects with dynamic keys using Lodash?

I have a nested JSON that I'd like to search through using lodash. 我有一个嵌套的JSON,我想使用lodash进行搜索。 How can I get the root object from data if a search term I'm looking for is within certain keys, and with one of the keys being dynamic? 如果我要查找的搜索词在某些键中并且其中一个键是动态的,如何从data获取根对象?

For example, if I have: 例如,如果我有:

"data": [
    { 
        "name": "Bob's concourse"
        "activities": [
             {
                 "day": "Monday",
                 "routines":
                     {
                         "Biking": 
                         {
                             "details": "won 3 trophies"
                             "type": "road"
                         },
                         "Kayaking":
                         {
                             "details": "participated in 3 races"
                             "type": "rhythm"
                         }
                      }
                 }
             }
        ]
    },

    {..other_data_etc...},

]
  • activities can be [] ; activities可以是[] it's not guaranteed that it contains any data. 不能保证它包含任何数据。
  • routines keys are dynamic. routines键是动态的。 ie, Biking , Kayaking are dynamic strings. BikingKayaking是动态字符串。 It can be anything. 可以是任何东西。

If I want to search for an races (case insensitive), I want to search specifically in: 如果我要搜索races (不区分大小写),我想专门搜索:

  • data.name
  • data.activities.routines.* (the dynamic keys) data.activities.routines.* (动态键)
  • data.activities.routines.*.details

If any one of those matches, then it will return the root object: { "name": "Bob", ..... } 如果其中任何一个匹配,则它将返回根对象: { "name": "Bob", ..... }

I was able to get the name to return: 我能够得到返回的name

function searchText(collection, searchterm) {
    return _.filter(collection, function(o) { 
        return _.includes(o.name.toLowerCase(), searchterm)
    } );
};

But I'm still new to lodash, and I was unable to get any of the nested searches to return correctly, especially with the dynamic keys part. 但是我对lodash还是很陌生,而且我无法使任何嵌套搜索正确返回,尤其是在动态键部分。

Could anyone help explain a solution? 谁能帮助解释解决方案?

Expanding on your existing attempt with lodash: 使用lodash扩展您现有的尝试:

 const obj = { data: [{ name: 'Bob\\'s concourse', activities: [{ day: 'Monday', routines: { Biking: { details: 'won 3 trophies', type: 'road' }, Kayaking: { details: 'participated in 3 races', type: 'rhythm' } } }] }] }; function search(str, data) { const searchStr = str.toLowerCase(); // Only return the entries that contain a matched value return _.filter(data, (datum) => { // Check if name matches return _.includes(datum.name, searchStr) || _.some(datum.activities, (activity) => { return _.entries(activity.routines).some(([routine, {details}]) => { // Check if dynamic routine matches or details return _.includes(routine, searchStr) || _.includes(details, searchStr); }); }); }); } console.log(search('foobar', obj.data)); console.log(search('races', obj.data)); 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script> 

You can also accomplish this with plain JavaScript. 您也可以使用纯JavaScript完成此操作。 Using some newish syntax such as destructuring assignment and newish native methods such as Object.entries essentially follows the same pattern as using lodash: 使用一些新的语法(例如, 销毁分配)和新的本机方法(例如, Object.entries基本上遵循与使用lodash相同的模式:

 const obj = { data: [{ name: 'Bob\\'s concourse', activities: [{ day: 'Monday', routines: { Biking: { details: 'won 3 trophies', type: 'road' }, Kayaking: { details: 'participated in 3 races', type: 'rhythm' } } }] }] }; function search(str, data) { const regex = RegExp(str, 'i'); // Only return the entries that contain a matched value return data.filter((datum) => { // Check if name matches return regex.test(datum.name) || datum.activities.some((activity) => { return Object.entries(activity.routines).some(([routine, {details}]) => { // Check if dynamic routine matches or details return regex.test(routine) || regex.test(details); }); }); }); } console.log(search('foobar', obj.data)); console.log(search('races', obj.data)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM