将属性从子级复制到父级?

时间:2018-07-16 14:59:43

标签: javascript

鉴于以下数据,我将如何遍历它,将parent.parentName从子(driver)复制到父(businessArea)并从type复制到{{ 1}}?

我通常可以弄清楚这样的数据操作,但是对于想法我一无所知,因为我无法productmap遍历数据(因为它是一个对象)。如果可能的话,我也试图使其保持功能。

forEach

所需结果示例:

每个父元素将获得一个新元素{ "businessArea": [ { "id": 1, "displayOrder": 10, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false }, { "id": 2, "displayOrder": 20, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false }, { "id": 3, "displayOrder": 30, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false } ], "driver": [ { "id": 1, "displayOrder": 10, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false, "parent": { "id": 1, "parentName": "businessArea" } }, { "id": 2, "displayOrder": 20, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false, "parent": { "id": 2, "parentName": "businessArea" } }, { "id": 3, "displayOrder": 30, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false, "parent": { "id": 2, "parentName": "businessArea" } } ], "product": [ { "id": 1, "displayOrder": 10, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false }, { "id": 2, "displayOrder": 20, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false }, { "id": 3, "displayOrder": 30, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false } ], "type": [ { "id": 1, "displayOrder": 10, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false, "parent": { "id": 1, "parentName": "product" } }, { "id": 2, "displayOrder": 20, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false, "parent": { "id": 1, "parentName": "product" } }, { "id": 3, "displayOrder": 30, "code": null, "description": "Lorem ipsum dolor sit amet", "isObsolete": false, "parent": { "id": 2, "parentName": "product" } } ] } ,该元素将指向子元素中childName所指示的正确子元素。

parentName

2 个答案:

答案 0 :(得分:0)

我认为这可以满足您的要求

['type','driver'].forEach(k=>{
   data[k].forEach(({parent})=>{
      const match = data[parent.parentName].find(({id})=> id===parent.id)
      match.childName = k; // TODO: make sure match exists first
   });
});

console.log(data)
.as-console-wrapper {max-height: 100%!important;}
<script>
  var data = {
    "businessArea": [{
        "id": 1,
        "displayOrder": 10
      },
      {
        "id": 2,
        "displayOrder": 20
      },
      {
        "id": 3,
        "displayOrder": 30
      }
    ],
    "driver": [{
        "id": 1,
        "displayOrder": 10,
        "parent": {
          "id": 1,
          "parentName": "businessArea"
        }
      },
      {
        "id": 2,
        "displayOrder": 20,
        "parent": {
          "id": 2,
          "parentName": "businessArea"
        }
      },
      {
        "id": 3,
        "displayOrder": 30,
        "parent": {
          "id": 2,
          "parentName": "businessArea"
        }
      }
    ],
    "product": [{
        "id": 1,
        "displayOrder": 10
      },
      {
        "id": 2,
        "displayOrder": 20
      },
      {
        "id": 3,
        "displayOrder": 30
      }
    ],
    "type": [{
        "id": 1,
        "displayOrder": 10,
        "parent": {
          "id": 1,
          "parentName": "product"
        }
      },
      {
        "id": 2,
        "displayOrder": 20,
        "parent": {
          "id": 1,
          "parentName": "product"
        }
      },
      {
        "id": 3,
        "displayOrder": 30,
        "parent": {
          "id": 2,
          "parentName": "product"
        }
      }
    ]
  }
</script>

答案 1 :(得分:0)

obj为对象时,尝试以下代码:

var obj = {
    "businessArea": [
        {
            "id": 1,
            "displayOrder": 10,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false
        },
        {
            "id": 2,
            "displayOrder": 20,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false
        },
        {
            "id": 3,
            "displayOrder": 30,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false
        }
    ],
    "driver": [
        {
            "id": 1,
            "displayOrder": 10,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false,
            "parent": {
                "id": 1,
                "parentName": "businessArea"
            }
        },
        {
            "id": 2,
            "displayOrder": 20,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false,
            "parent": {
                "id": 2,
                "parentName": "businessArea"
            }
        },
        {
            "id": 3,
            "displayOrder": 30,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false,
            "parent": {
                "id": 2,
                "parentName": "businessArea"
            }
        }
    ],
    "product": [
        {
            "id": 1,
            "displayOrder": 10,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false
        },
        {
            "id": 2,
            "displayOrder": 20,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false
        },
        {
            "id": 3,
            "displayOrder": 30,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false
        }
    ],
    "type": [
        {
            "id": 1,
            "displayOrder": 10,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false,
            "parent": {
                "id": 1,
                "parentName": "product"
            }
        },
        {
            "id": 2,
            "displayOrder": 20,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false,
            "parent": {
                "id": 1,
                "parentName": "product"
            }
        },
        {
            "id": 3,
            "displayOrder": 30,
            "code": null,
            "description": "Lorem ipsum dolor sit amet",
            "isObsolete": false,
            "parent": {
                "id": 2,
                "parentName": "product"
            }
        }
    ]
};

for (var prop in obj) {
  var arr = obj[prop];
  for (var item in arr) {
    if (undefined !== arr[item].parent) {
      var parent = obj[arr[item].parent.parentName];
      for (var parentItem in parent) {
        if (parent[parentItem].id === arr[item].parent.id) {
          parent[parentItem].childName = prop;
          break;
        }
      }
    }
  }
}

console.log(obj);