gpt4 book ai didi

javascript - 如何在javascript中合并具有相同值的json数组

转载 作者:行者123 更新时间:2023-11-29 19:23:15 25 4
gpt4 key购买 nike

我有两个数组,我想合并具有相同值的数据

"calleridnum":"0090000163","uid":"0090000163",

这是我的第一个数组:

[
{
"_id":"55d44fe38879f12e6431c23c",
"event":"ConfbridgeJoin",
"channel":"SIP/peer_voip301confbridge-00000145",
"uniqueid":"1439977429.777",
"conference":"0090000167",
"calleridnum":"0090000163",
"calleridname":"0090000163",
"__v":0,
"sipSetting":{
"accountcode":"",
"accountcode_naisen":"202",
"extentype":0,
"extenrealname":"",
"name":"0090000163",
"secret":"Myojyo42f!",
"username":"0090000163",
"context":"innercall_xdigit",
"gid":101,
"cid":"0090000007"
}
}
]

这是我的第二个数组

[
{
"id":8,
"uid":"0090000163",
"cid":"0090000007",
"extension":"202",
"secret":"Myojyo42f!",
"leader":true,
"simultaneous":false,
"confbridge_id":6,
"created_at":"2015-08-18 09:22:20",
"updated_at":"2015-08-18 09:22:20"
},
{
"id":15,
"uid":"0090000164",
"cid":"0090000007",
"extension":"203",
"secret":"Myojyo42f!",
"leader":false,
"simultaneous":false,
"confbridge_id":6,
"created_at":"2015-08-19 01:26:30",
"updated_at":"2015-08-19 01:26:30"
}
]

我想合并具有相同值0090000163的对象

我想获取 if calleridnum == uid

我想得到这个输出

"_id":"55d44fe38879f12e6431c23c",
"event":"ConfbridgeJoin",
"channel":"SIP/peer_voip301confbridge-00000145",
"uniqueid":"1439977429.777",
"conference":"0090000167",
"calleridnum":"0090000163",
"calleridname":"0090000163",
"__v":0,
"sipSetting":{
"accountcode":"",
"accountcode_naisen":"202",
"extentype":0,
"extenrealname":"",
"name":"0090000163",
"secret":"Myojyo42f!",
"username":"0090000163",
"context":"innercall_xdigit",
"gid":101,
"cid":"0090000007"
"id":8,
"uid":"0090000163",
"cid":"0090000007",
"extension":"202",
"secret":"Myojyo42f!",
"leader":true,
"simultaneous":false,
"confbridge_id":6,
"created_at":"2015-08-18 09:22:20",
"updated_at":"2015-08-18 09:22:20"

我用 php 来做。但我对 javascript 了解不多

这是我的 php 代码

// $apiResults['participants'] == first array
// $apiResults['conference_participants'] == second array


$conf_participants = array();
foreach($apiResults['participants'] as $participant)
{
foreach($apiResults['conference_participants'] as $conference_participant)
{
if($participant['calleridnum'] == $conference_participant['uid'])
{

$conf_participants['uniqueid'] = $participant['uniqueid'];
$conf_participants['event'] = $participant['event'];
$conf_participants['channel'] = $participant['channel'];
$conf_participants['uid'] = $conference_participant['uid'];



}
}
}

最佳答案

假设您有两个对象数组 arr1arr2 您需要遍历这两个对象,比较 中的值 calleridnum arr1arr2 中的uid 如果它们相同则合并对象in arr1

for(var i in arr1){
for (var j in arr2)
if (arr2[j].uid == arr1[i].calleridnum ) {
$.extend(arr1[i], arr2[j]);
break;
}
}

关于javascript - 如何在javascript中合并具有相同值的json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32133714/

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