javascript - 递归解析对象数组并根据 id 过滤对象

标签 javascript node.js rhino

我有这个对象数组:getCategory(变量)

[
  {
    "id": "20584",
    "name": "Produits de coiffure",
    "subCategory": [
      {
        "id": "20590",
        "name": "Coloration cheveux",
        "subCategory": [
          {
            "id": "20591",
            "name": "Avec ammoniaque"
          },
          {
            "id": "20595",
            "name": "Sans ammoniaque"
          },
          {
            "id": "20596",
            "name": "Soin cheveux colorés"
          },
          {
            "id": "20597",
            "name": "Protection"
          },
          {
            "id": "20598",
            "name": "Nuancier de couleurs"
          }
        ]
      },
      {
        "id": "20593",
        "name": "Soins cheveux",
        "subCategory": [
          {
            "id": "20594",
            "name": "Shampooing"
          },
          {
            "id": "20599",
            "name": "Après-shampooing"
          },
          {
            "id": "20600",
            "name": "Masques"
          },

我尝试了在 stackoverflow 中可以搜索的所有内容..

让我们说在这个数组上我想递归地获取具有指定id的对象..就像20596并且它应该返回

{
            "id": "20596",
            "name": "Soin cheveux colorés"
          }

我正在做的逻辑方式是这样的:

var getSubcategory = getCategory.filter(function f(obj){
        if ('subCategory' in obj) {
            return obj.id == '20596' || obj.subCategory.filter(f);
        }
        else {
            return obj.id == '20596';
        }
    });

不知道还能做什么。 谢谢

PS:我不在浏览器中使用它,所以我无法使用任何库。只是服务器端,没有其他库。 find 不起作用,所以我只能使用 filter

最佳答案

您需要返回找到的对象。

function find(array, id) {
    var result;
    array.some(function (object) {
        if (object.id === id) {
            return result = object;
        }
        if (object.subCategory) {
            return result = find(object.subCategory, id);
        }
    });
    return result;
}

var data = [{ id: "20584", name: "Produits de coiffure", subCategory: [{ id: "20590", name: "Coloration cheveux", subCategory: [{ id: "20591", name: "Avec ammoniaque" }, { id: "20595", name: "Sans ammoniaque" }, { id: "20596", name: "Soin cheveux colorés" }, { id: "20597", name: "Protection" }, { id: "20598", name: "Nuancier de couleurs" }] }, { id: "20593", name: "Soins cheveux", subCategory: [{ id: "20594", name: "Shampooing" }, { id: "20599", name: "Après-shampooing" }, { id: "20600", name: "Masques" }] }] }];
  
console.log(find(data, '20596'));
console.log(find(data, ''));

关于javascript - 递归解析对象数组并根据 id 过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45214266/

相关文章:

javascript - Facebook 应用程序不提供用户名

javascript - 无法让 JQuery $.ajax() 完全同步工作

javascript - protobuf.js : print int64 Object as string in node. js

java - 找不到 RhinoScriptEngineFactory

javascript - React js 中的自调用函数? (与常规 javascript 一样)

javascript - 调整动态加载的 MediaElementjs 视频的大小

node.js - 捕获流程的连续输出

javascript - Node.js/Socket.io 使用安全连接在 IE 中不返回结果

javascript - Rhino 性能和编译脚本

java - YuiCompressorTask : "Can' t find bundle for base name org. mozilla.javascript.resources.Messages,区域设置 en_US"