gpt4 book ai didi

javascript - 如何通过匹配id将数组插入对象

转载 作者:行者123 更新时间:2023-11-28 13:19:17 25 4
gpt4 key购买 nike

我从 api 返回一个对象数组,其中包含 currentDataResult 和 PastDataResult。

[
{
"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
}
]
}
]

我需要将匹配的过去数据对象(按section_id)对象作为嵌套数组推送到当前数据结果对象中。这就是它需要的样子

"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....
]

我创建了一个服务,它获取当前和过去的数据结果,并对过去的数据结果重新排序以匹配当前的数据结果。我需要帮助的是将过去的数据数组插入当前的数据对象。现在,它错误地将整个过去的数据数组插入当前数据数组的第一个对象中。我已经用我的代码设置了一个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;

});

最佳答案

这是您要找的吗?

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;

});

输出:

[
{
"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
}
},

等等

关于javascript - 如何通过匹配id将数组插入对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34812670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com