比较键后如何将对象列表转换为嵌套对象

时间:2019-06-04 06:28:04

标签: javascript jquery angular object

目标:根据对象的父值将对象列表放入嵌套对象中。

以下是我的const products_schema = { _id: { auto: true }, product_name: { auto: false, type: "string", min: 5, max: 10, special_characters: ['_', ' '], numbers: true, alphabet: true, required: true, correct: "" }, product_image: { auto: false, type: "array:string", min: 0, max: 50, required: true }, product_specification: { auto: false, type: "array:specification_schema", min: 0, max: 50, required: true } } } let schema=new Map() schema.set('products_schema',products_schema) for([key,value] of schema.entries()){ console.log(value.type) //shows undefined in the console } 数据:

json

在每个具有 id和依赖项

的对象中
"data": [
    {
      "id": "coding-825x500",
      "source": {
        "information": {
          "fileid": "coding-825x500",
          "filesize": 67340
        },
        **"dependent": "d1bc270d"**
      }
    },
    {
      "id": "d1bc270d",
      "source": {
        "information": {
          "fileid": "d1bc270d",
          "filesize": 193
        },
        "dependent": "parent"
      }
    },
    {
      "id": "1_iwPLQ",
      "source": {
        "information": {
          "fileid": "1_iwPLQ",
          "filesize": 580969
        },
        "dependent": "d1bc270d"
      }
    },
    {
      "id": "coding-825",
      "source": {
        "information": {
          "fileid": "coding-825",
          "filesize": 67340
        },
        "dependent": null
      }
    }
  ]
}

如果id等于从属id,则它应该是子代,如果从属是父代,则{ "id": A "dependent":parent }, { "id": B "dependent":A }, { "id": C "dependent":A }, { "id": D "dependent":null } 必须在此之下,如果从属为null,则它也是没有子代的父代孩子们。

下面我使用过滤器,但是后来我不确定如何继续创建嵌套对象。

id == dependent

Stackblitz => https://stackblitz.com/edit/angular-zvcea7

所需的输出:所有子对象都应位于父对象下以设置嵌套数据,该表可能类似于以下格式。

let info = this.dynamic.data.filter((val)=>{
     console.log(val.id, ":::" ,val.source.dependent);
   })

2 个答案:

答案 0 :(得分:1)

您可以构建一棵树,并将其用于'parent'且具有统一null值的节点。

这种方法也适用于未排序的数据。

var data = [{ id: 'A', dependent: 'parent' }, { id: 'B', dependent: 'A' }, { id: 'D', dependent: null }, { id: 'C', dependent: 'A' }],
    tree = function (data) {
        var t = {};
        data.forEach(o => {
            var parent = o.dependent === 'parent' ? null : o.dependent;
            Object.assign(t[o.id] = t[o.id] || {}, o);
            t[parent] = t[parent] || {};
            t[parent].nested = t[parent].nested || [];
            t[parent].nested.push(t[o.id]);
        });
        return t.null.nested;
    }(data);

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

使用第一个数据集。 dependent属性嵌套在另一个对象中。

var data = [{ id: "coding-825x500", source: { information: { fileid: "coding-825x500", filesize: 67340 }, dependent: "d1bc270d" } }, { id: "d1bc270d", source: { information: { fileid: "d1bc270d", filesize: 193 }, dependent: "parent" } }, { id: "1_iwPLQ", source: { information: { fileid: "1_iwPLQ", filesize: 580969 }, dependent: "d1bc270d" } }, { id: "coding-825", source: { information: { fileid: "coding-825", filesize: 67340 }, dependent: null } }],
    tree = function (data) {
        var t = {};
        data.forEach(o => {
            var parent = o.source.dependent === 'parent' ? null : o.source.dependent;
            Object.assign(t[o.id] = t[o.id] || {}, o);
            t[parent] = t[parent] || { id: parent, source: null };
            t[parent].nested = t[parent].nested || [];
            t[parent].nested.push(t[o.id]);
        });
        return t.null.nested;
    }(data);

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

答案 1 :(得分:1)

好吧,您可以使用数组约简功能来达到目的。

var data = [{"id": "coding-825x500","source": {"information": {"fileid": "coding-25x500","filesize": 67340},"dependent": "d1bc270d"}},{"id": "d1bc270d","source": {"information": {"fileid": "d1bc270d","filesize": 193},"dependent": "parent"}},{"id": "1_iwPLQ","source": {"information": {"fileid": "1_iwPLQ","filesize": 580969},"dependent": "d1bc270d"}},{"id": "coding-825","source": {"information": {"fileid": "coding-825","filesize": 67340},"dependent": null}}];
var result = data.reduce(function(acc, elem) {
    if( elem.source.dependent === 'parent') {
      var dependents = data.filter(function(a) { return a.source.dependent ===elem.id});
      if( dependents && dependents.length ) {
          elem.nested = elem.nested || [];
          (dependents || []).forEach(function(d) {
              elem.nested.push(d);
          })
         acc.push(elem);
      } else {
          acc.push(elem);
      }
  } else if( elem.source.dependent === null) {
      acc.push(elem);
  }
  return acc;
}, []);

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

PS。没有最大程度地优化这一点。