简体   繁体   English

尝试使用Lodash返回深层嵌套对象

[英]Trying to return a deep nested object with Lodash

I have not been able to find a solution for this on StackoverlFlow and i have been working on this issue for some time now. 我无法在StackoverlFlow上找到针对此问题的解决方案,并且我已经致力于此问题已有一段时间了。 Its a bit difficult so please let me know if i should provide more information. 这有点困难,所以请让我知道我是否应该提供更多信息。

My problem: I have a JSON object of Lands, each land has an ID and a Blocks array associated with it and that blocks array has block with ID's too. 我的问题:我有一个Lands的JSON对象,每个土地都有一个ID和与之关联的Blocks数组,并且blocks数组也具有ID的block。

Preview: 预习:

var Lands = [{
                'LandId':'123',
                'something1':'qwerty',
                'something2':'qwerty',
                'Blocks': [
                    {
                        'id':'456',
                        'LandId':'123'
                    },
                    {
                        'BlockId':'789',
                        'LandId':'123'
                    }
                ]
            },
            ...More Land Objects];

Note : The data is not setup the way i would have done it but this was done along time ago and i have to work with what i have for right now. 注意 :数据不是按照我本来会设置的方式设置的,但是这是在一段时间之前完成的,因此我现在必须处理我所拥有的东西。

I am trying to write a lodash function that will take the blockId's that i have and match them and return the landId from the Blocks. 我试图编写一个lodash函数,该函数将获取我拥有的blockId并将其匹配,并从Blocks返回landId。

so the end result would be a list of LandId's that where returned from the Blocks. 因此最终结果将是从块返回的LandId列表。

I was using something like this and it was returning no results: 我正在使用类似的东西,但没有返回结果:

selectedLand = function(Lands, landIDs){
                return _.filter(Lands, function(land){
                    return land === landIDs[index];
                });
            };

I know i am going about this the wrong way and would like to know the appropriate way to approach this and solve it. 我知道我正在以错误的方式处理此问题,并且想知道解决此问题的适当方法。 any help is much appreciated. 任何帮助深表感谢。

selectedLand = function(Lands, landIDs){
                return _.filter(Lands, function(land){
                    return land === landIDs[index];
                });
            };

Note that index lacks any definition in this scope, so this function will essentially never return true, barring a lucky(?) accident with an externally defined index . 请注意, index在此范围内没有任何定义,因此,除非使用外部定义的index发生lucky(?)事故,否则此函数将永远不会返回true。 And anytime you call _.filter(someArr,function(){return false;}) you'll get [] . 每当您调用_.filter(someArr,function(){return false;})您都会得到[] Undefined index aside, this does strict comparison of a land object against (maybe) a string in landIDs 除了未定义的index外,这会严格比较Land对象与(可能是)landID中的字符串

I'm a bit unclear on the exact requirements of your selection, so you can tailor this filter to suit your needs. 我对您选择的确切要求尚不清楚,因此您可以定制此过滤器以适合您的需求。 The function filters the lands array by checking if the .blocks array property has some values where the .landID property is included in the landsID array. 函数滤波 lands通过检查阵列如果.blocks数组属性具有其中的一些.landID属性被包括landsID阵列。

In closing, if you want to make the most out of lodash (or my favorite, ramda.js ) I suggest you sit down and read the docs . 最后,如果您想充分利用lodash (或我最喜欢的ramda.js ),我建议您坐下来阅读文档 Sounds deathly boring, but 75% of the battle with data transforms is knowing what's in your toolbox. 听起来很无聊,但是75%的数据转换之争是知道工具箱中的内容。 Note how the English description of the process matches almost exactly with the example code (filter, some, includes). 请注意,该过程的英文说明几乎与示例代码(过滤器,有些包含)完全匹配。

 var Lands = [{ 'LandId': '123', 'something1': 'qwerty', 'something2': 'qwerty', 'Blocks': [{ 'id': '456', 'LandId': '123' }, { 'BlockId': '789', 'LandId': '123' }] } ]; // assuming lands is like the above example // and landIDs is an array of strings var selectLandsWithBlocks = function(lands, landIDs) { return _.filter(lands, function(land) { var blocks = land.Blocks; var blockHasALandId = function(block) { return _.includes(landIDs,block.LandId); }; return _.some(blocks, blockHasALandId); }); }; console.log(selectLandsWithBlocks(Lands,[])); console.log(selectLandsWithBlocks(Lands,['mittens'])); console.log(selectLandsWithBlocks(Lands,['123'])); 
 <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script> 

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

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