简体   繁体   English

如何通过匹配ID将数组推入对象

[英]how to push array into object by matching ids

I am returning a object array from a api that contains a currentDataResult and a pastDataResult. 我要从包含currentDataResult和pastDataResult的api返回一个对象数组。

[
{
    "sectionCurrentDataResult": [
        {
            "section_id": 14785,
            "subdivision_name": "Stratton Woods",
        },
        {
            "section_id": 14790,
            "subdivision_name": "Stratton Woods",
        },
        {
            "section_id": 14791,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14792,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14781,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14786,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14787,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14788,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14782,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14783,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 14784,
            "subdivision_name": "Stratton Woods"
        },
        {
            "section_id": 5326,
            "subdivision_id": 1439,
            "subdivision_name": "Stratton Woods"
        }
    ]
},
{
    "sectionPastDataResult": [
        {
            "section_id": 5326,
            "price_min": 177,
            "price_max": 235
        },
        {
            "section_id": 14785,
            "price_min": 190,
            "price_max": 220
        },
        {
            "section_id": 14786,
            "price_min": 238,
            "price_max": 292
        },
        {
            "section_id": 14788,
            "price_min": 186,
            "price_max": 205
        },
        {
            "section_id": 14790,
            "price_min": 150,
            "price_max": 269
        },
        {
            "section_id": 14783,
            "price_min": 150,
            "price_max": 260
        },
        {
            "section_id": 14787,
            "price_min": 90,
            "price_max": 90
        },
        {
            "section_id": 14792,
            "price_min": 177,
            "price_max": 235
        },
        {
            "section_id": 14791,
            "price_min": 145,
            "price_max": 221
        },
        {
            "section_id": 14784,
            "price_min": 148,
            "price_max": 186
        },
        {
            "section_id": 14781,
            "price_min": 155,
            "price_max": 200
        },
        {
            "section_id": 14782,
            "price_min": 150,
            "price_max": 170
        }
      ]
    }
  ]

I need to push the matching pastDataObject(by section_id) object into the currentDataResult object as a nested array. 我需要将匹配的pastDataObject(by section_id)对象作为嵌套数组推入currentDataResult对象。 this is what it needs to look like 这就是它需要的样子

"sectionCurrentDataResult": [
        {
            "section_id": 14785,
            "subdivision_name": "Stratton Woods",
            "sectionHistory":[{
               "section_id": 14785,
               "price_min": 190,
               "price_max": 220
            }]
        },
        {
            "section_id": 14790,
            "subdivision_name": "Stratton Woods",
            "sectionHistory":[{
               "section_id": 14790,
               "price_min": 150,
               "price_max": 269
            }]
        },
        etc....
        ]

I have created a service that takes both the current and past data result and re-orders the past data result to match the current. 我创建了一个服务,该服务同时获取当前和过去的数据结果,并对过去的数据结果进行重新排序以匹配当前的结果。 what i need help with is pushing the past data array into the current data object. 我需要帮助的是将过去的数据数组推送到当前数据对象中。 right now it is incorrectly pushing the entire past data array into the first object into the current data array.I have setup a plunker with my code. 现在它错误地将整个过去的数据数组推入第一个对象到当前数据数组。我已经用代码设置了一个插件。

plunker plunker

app.controller('MainCtrl', function($scope,bigEnchilada,inputHistorySvc) {

   for (var i = 0; i < bigEnchilada[0].sectionCurrentDataResult.length; i++) {
        bigEnchilada[0].sectionCurrentDataResult[i].sectionHistory =  inputHistorySvc.historyInputs(bigEnchilada);
        }
      $scope.sections = bigEnchilada[0].sectionCurrentDataResult;

    });

is this what you're looking for? 这是您要找的东西吗?

var app = angular.module('angularjs-starter', []);

app.controller('MainCtrl', function($scope,bigEnchilada,inputHistorySvc) {
var list = inputHistorySvc.historyInputs(bigEnchilada);
for (var i = 0; i < bigEnchilada[0].sectionCurrentDataResult.length; i++){
   for (var j = 0; j < list.length; j++ ) {
      if(bigEnchilada[0].sectionCurrentDataResult[i].section_id ==      list[j].section_id){
          bigEnchilada[0].sectionCurrentDataResult[i].sectionHistory = list[j];
      }
   }
}
  $scope.sections = bigEnchilada[0].sectionCurrentDataResult;

});

Output: 输出:

[
{
"section_id": 14785,
"subdivision_name": "Stratton Woods",
"sectionHistory": {
  "section_id": 14785,
  "price_min": 190,
  "price_max": 220
}
},
{
"section_id": 14790,
"subdivision_name": "Stratton Woods",
"sectionHistory": {
  "section_id": 14790,
  "price_min": 150,
  "price_max": 269
}
},
 {
"section_id": 14791,
"subdivision_name": "Stratton Woods",
"sectionHistory": {
  "section_id": 14791,
  "price_min": 145,
  "price_max": 221
}
},

etc. 等等

var inputHistory = inputHistorySvc.historyInputs(bigEnchilada);
  for (var i = 0; i < bigEnchilada[0].sectionCurrentDataResult.length; i = i + 1) {
      for (x = 0; x < inputHistory.length; x = x + 1) {
        if (bigEnchilada[0].sectionCurrentDataResult[i].section_id === inputHistory[x].section_id) {
          bigEnchilada[0].sectionCurrentDataResult[i].sectionHistory = inputHistory[x];
        }
      }
      }
  $scope.sections = bigEnchilada[0].sectionCurrentDataResult;

You can use a temporary object and two separated loops. 您可以使用一个临时对象和两个单独的循环。 One for getting all references and the second to assign the data to the reference. 一种用于获取所有引用,第二种用于将数据分配给该引用。 Big O in this solution: O(n) . 此解决方案中的Big OO(n)

 var data = [{ "sectionCurrentDataResult": [{ "section_id": 14785, "subdivision_name": "Stratton Woods", }, { "section_id": 14790, "subdivision_name": "Stratton Woods", }, { "section_id": 14791, "subdivision_name": "Stratton Woods" }, { "section_id": 14792, "subdivision_name": "Stratton Woods" }, { "section_id": 14781, "subdivision_name": "Stratton Woods" }, { "section_id": 14786, "subdivision_name": "Stratton Woods" }, { "section_id": 14787, "subdivision_name": "Stratton Woods" }, { "section_id": 14788, "subdivision_name": "Stratton Woods" }, { "section_id": 14782, "subdivision_name": "Stratton Woods" }, { "section_id": 14783, "subdivision_name": "Stratton Woods" }, { "section_id": 14784, "subdivision_name": "Stratton Woods" }, { "section_id": 5326, "subdivision_id": 1439, "subdivision_name": "Stratton Woods" }] }, { "sectionPastDataResult": [{ "section_id": 5326, "price_min": 177, "price_max": 235 }, { "section_id": 14785, "price_min": 190, "price_max": 220 }, { "section_id": 14786, "price_min": 238, "price_max": 292 }, { "section_id": 14788, "price_min": 186, "price_max": 205 }, { "section_id": 14790, "price_min": 150, "price_max": 269 }, { "section_id": 14783, "price_min": 150, "price_max": 260 }, { "section_id": 14787, "price_min": 90, "price_max": 90 }, { "section_id": 14792, "price_min": 177, "price_max": 235 }, { "section_id": 14791, "price_min": 145, "price_max": 221 }, { "section_id": 14784, "price_min": 148, "price_max": 186 }, { "section_id": 14781, "price_min": 155, "price_max": 200 }, { "section_id": 14782, "price_min": 150, "price_max": 170 }] }]; void function () { var o = {}; data[0].sectionCurrentDataResult.forEach(function (a) { o[a.section_id] = a; }); data[1].sectionPastDataResult.forEach(function (a) { o[a.section_id].sectionHistory = [a]; }); }(); document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>'); 

You can actually avoid the nested looping if you break this json , sort them by section_id and then just create the required object in a single loop. 如果破坏此json,实际上可以避免嵌套循环,请按section_id对其进行排序,然后仅在单个循环中创建所需的对象。 You can also avoid the if -else loop and equality check 您还可以避免if -else循环和equality check

     // Get sorted array of  sectionCurrentDataResult
var array1 = a[0]["sectionCurrentDataResult"].sort(function(x,y){
     return x.section_id>y.section_id? 1 : x.section_id<y.section_id? -1 :0; 
  })

 // Get sorted array of  sectionPastDataResult

var array2= a[1]["sectionPastDataResult"].sort(function(x,y){
     return x.section_id>y.section_id? 1 : x.section_id<y.section_id? -1 :0; 
  })

// Will be populated with merged data
var sectionCurrentDataResult=[]

  for(var a = 0;a<array1.length;a++){
      var sectionHistory=[];
      sectionHistory.push({
      "section_id":array2[a].section_id,
      "price_min":array2[a].price_min,
      "price_max":array2[a].price_max
      })
      sectionCurrentDataResult.push({
      "section_id":array1[a].section_id,
      "subdivision_name":array1[a].subdivision_name,
      "sectionHistory":sectionHistory
      })
  }
  console.log(sectionCurrentDataResult);

WORKING EXAMPLE 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM