【问题标题】:Merge and match objects that match AngularJS合并和匹配匹配 AngularJS 的对象
【发布时间】:2017-03-06 20:01:06
【问题描述】:

您好,我想合并和匹配两个 json 对象。我想使用 Angular 方式。任何帮助将不胜感激。谢谢大家。这是一个例子:

// 第一个json对象 { "用户 ID":"john.davis", “edpi”:空, “用户应用程序”:[ { "名称":"名称 3", "链接":"/Protect3.php" }, { "名称":"名称 5", "链接":"/Admin/Launch.html" }, { "名称":"名称 2", "链接":"aaap/defaultBH.php" } ] } // 第二个json对象 [ { “颜色”:“颜色1”, "icon": "content/images/icons/administrator.svg", “名称”:“名称 5” }, { “颜色”:“颜色3”, "icon": "content/images/icons/administrator.svg", “名称”:“名称 3” }, { “颜色”:“颜色2”, "icon": "content/images/icons/behavior.svg", “名称”:“名称 2” } ]

希望得到这个

[ { "TokenName":"name3", "TokenLink":"../Protect3.asp", “颜色”:{ “颜色”:“颜色3”, "icon": "content/images/icons/administrator.svg", “匹配”:“名称 3” } }, { "名称":"名称 5", "链接":"/Admin/Launch.html", “颜色”:{ “颜色”:“颜色1”, "icon": "content/images/icons/administrator.svg", “匹配”:“名称 5” } }, { "名称":"名称 2", “链接”:“aaap/defaultBH.php” “颜色”:{ “颜色”:“颜色2”, "icon": "content/images/icons/behavior.svg", “匹配”:“名称 2” } } ]

【问题讨论】:

  • 在问题中详细说明您要达到的目标
  • 在我当前的代码中,我已经成功合并了两个对象。但现在我想合并匹配的对象属性。下面是我合并两个对象的代码。
  • 首先我使用 Angular http 拉取了两个 json 对象。然后我编写了一个函数将对象从第二个对象推送到第一个对象。函数 addColor (objectArray, colorArray, restrictedtemplate){ objectArray.forEach(function(object, index){ object.color = colorArray[index]; }); }; addColor($scope.restrictedapps, $scope.colors, $scope.unrestricteddata);

标签: javascript angularjs json


【解决方案1】:

我想通了。这是下面的代码。

函数 mergeArraysOnProperty(array1, array2, property) { array1.forEach(函数(element1) { array2.forEach(函数(元素 2){ if (element1[property] === element2[property]) { for (var newProperty in element2) { if (element2.hasOwnProperty(newProperty)) { 元素1[新属性] = 元素2[新属性]; } } } }); }); 返回数组1; }; $scope.newjson = mergeArraysOnProperty(userapp, color, "Name"); 控制台.log($scope.newjson);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 2019-11-13
    • 1970-01-01
    • 2022-11-24
    • 2019-01-25
    • 2021-01-29
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多