JS:将点字符串数组转换为对象树 [英] JS: Convert dot string array to object tree

查看:32
本文介绍了JS:将点字符串数组转换为对象树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串数组,并试图构建一个以 .符号.我尝试使用 lodash 的递归函数和设置"函数,但没有得到预期的结果.

let stringArray = ['目录.产品.名称','catalog.product.description','catalog.product.status','catalog.product_attribute_set.name''catalog.product_attribute_set.type']

我的预期结果是这样的

<预><代码>[{"值":"目录","标签":"目录",孩子们":[{"价值":"产品","标签":"产品",孩子们":[{"值":"名称",标签":名称"},{"值":"描述",标签":说明"},{"值":"状态",标签":状态"}]},{"value":"product_attribute_set","label":"product_attribute_set",孩子们":[{"值":"名称",标签":名称"},{值类型",标签":类型"}]}]}]

解决方案

您可以拆分字符串并将这些部分用作嵌套数组的路径.

var array = ['catalog.product.name', 'catalog.product.description', 'catalog.product.status', 'catalog.product_attribute_set.name', 'catalog.product_attribute_set.type'],结果 = [];array.forEach(s => s.分裂('.').reduce((object, value) => {var item = (object.children = object.children || []).find(q => q.value === value);if (!item) object.children.push(item = { value, label: value })归还物品;}, { 孩子:结果 }));console.log(result);

.as-console-wrapper { max-height: 100% !important;顶部:0;}

I have a string array like this and trying to build a tree hierarch grouped with . notation. i tried using recursive function and "set" function from lodash but could not get expected result.

let stringArray = [
  'catalog.product.name', 
  'catalog.product.description', 
  'catalog.product.status', 
  'catalog.product_attribute_set.name'
  'catalog.product_attribute_set.type'
]

my expected result is like this

[
   {
      "value":"catalog",
      "label":"catalog",
      "children":[
         {
            "value":"product",
            "label":"product",
            "children":[
               {
                  "value":"name",
                  "label":"name"
               },
               {
                  "value":"description",
                  "label":"description"
               },
               {
                  "value":"status",
                  "label":"status"
               }
            ]
         },
         {
            "value":"product_attribute_set",
            "label":"product_attribute_set",
            "children":[
               {
                  "value":"name",
                  "label":"name"
               },
               {
                  "value":"type",
                  "label":"type"
               }
            ]
         }
      ]
   }
]

解决方案

You could split the strings and use the parts as path to the nested array.

var array = ['catalog.product.name', 'catalog.product.description', 'catalog.product.status', 'catalog.product_attribute_set.name', 'catalog.product_attribute_set.type'],
    result = [];

array.forEach(s => s
    .split('.')
    .reduce((object, value) => {
        var item = (object.children = object.children || []).find(q => q.value === value);
        if (!item) object.children.push(item = { value, label: value })
        return item;
    }, { children: result })
);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于JS:将点字符串数组转换为对象树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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