简体   繁体   English

javascript 中嵌套 object 的搜索功能

[英]Search functionality on nested object in javascript

在此处输入图像描述

The requirement is such, return the categories name on the top level if the search value matches with the dishes name inside dishes array .要求是这样的,如果搜索值与dishes name inside dishes array中的菜名匹配,则返回顶层的categories name

If I search a dishes name here for example if I search name = veg mulligatawany soup then it should return the categories name = Soups .如果我在这里搜索菜肴名称,例如如果我搜索name = veg mulligatawany soup那么它应该返回categories name = Soups

在此处输入图像描述 depthSearch return empty array here. depthSearch 在这里返回空数组。

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks谢谢

I hope I can help with this.我希望我能帮助解决这个问题。 I may have this question backwards as your question is not formatted very well and I feel I'm lacking some information.我可能有这个问题,因为你的问题的格式不是很好,我觉得我缺乏一些信息。 Nevertheless, I have a CodePen that may help.不过,我有一个可能会有所帮助的 CodePen。 This CodePen demonstrates retrieving this information that you asked for.此 CodePen 演示如何检索您要求的信息。 However, I purposefully didn't provide a complete solution.但是,我故意没有提供完整的解决方案。 Instead I opted to demonstrate how you would search for the value and return it.相反,我选择演示如何搜索该值并返回它。 It will be up to you to integrate and make this solution work for you.集成并使此解决方案适合您将取决于您。

But, before you take the code, let's learn a thing or two.但是,在获取代码之前,让我们先学习一两件事。 After all, we all need to learn more and become more effective as developers.毕竟,作为开发人员,我们都需要学习更多并变得更有效率。 Let's begin.让我们开始。

You can refer to this resource as we go: https://medium.com/@jeff_long/understanding-foreach-map-filter-and-find-in-javascript-f91da93b9f2c您可以参考此资源为我们 go: https://medium.com/@jeff_long/understanding-foreach-map-filter-and-find-in-javascript-f91da93b9f2c

You used filter functions in your code sample.您在代码示例中使用了过滤器函数。 (please format as code in the future and not as an image) There are great when you are assigning a new array from an existing array in which you filter the results. (请在将来格式化为代码而不是图像)当您从现有数组中分配一个新数组时,您可以在其中过滤结果。 For example,例如,

const _arr = ['1', '2', '3']
const _arr2 = _arr.filter(val => val !== '2') // ['1', '3']

However, this function returns data and therefore takes up more memory space when executed.但是,这个 function 返回数据,因此在执行时会占用更多的 memory 空间。 This function is useful when you are cloning or creating datasets from another array where you need the data in a new variable.这个 function 在您从另一个数组中克隆或创建数据集时非常有用,您需要将数据包含在新变量中。

I used.forEach because this function doesn't return a new object like.filter does.我使用了.forEach,因为这个 function 不会像.filter 一样返回新的 object。 Instead it allows you to do operations on the contents that you are iterating through.相反,它允许您对正在迭代的内容进行操作。 For example, you could print out the contents in a console.log statement.例如,您可以打印出 console.log 语句中的内容。 Or, you can trigger side effects in your code.或者,您可以在代码中触发副作用。 Or, in my case, you can compare and set/update variables outside of the scope of your.forEach function.或者,在我的情况下,您可以比较和设置/更新 your.forEach function 的 scope 之外的变量。 This function results in less memory used typically and lends itself to more performant code.此 function 导致通常使用的 memory 较少,并有助于提高代码的性能。

It seems that you already picked up on making the search non-case-sensitive.您似乎已经开始着手使搜索不区分大小写。 That's good, However.这很好,但是。 you missed lower-casing your search value.您错过了小写搜索值。 Lower-casing this value too will prevent searches such as 'Bacon' === 'bacon' // false小写这个值也将阻止搜索,例如'Bacon' === 'bacon' // false

I have attached the CodePen below as promised.我已按照承诺附上了下面的 CodePen。 Let me know if this is NOT what you were looking for and I misunderstood the problem.如果这不是您想要的,请告诉我,我误解了这个问题。 Happy coding!快乐编码!

https://codepen.io/brentvdalling/pen/xxWvbgy?editors=0011 https://codepen.io/brentvdalling/pen/xxWvbgy?editors=0011

// sources that backup my claims // 支持我的声明的来源

https://gomakethings.com/how-performant-are-modern-array-methods-vs-old-school-for-loops-in-vanilla-js/ https://gomakethings.com/how-performant-are-modern-array-methods-vs-old-school-for-loops-in-vanilla-js/

https://medium.com/@jeff_long/understanding-foreach-map-filter-and-find-in-javascript-f91da93b9f2c https://medium.com/@jeff_long/understanding-foreach-map-filter-and-find-in-javascript-f91da93b9f2c

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

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