I have two array and i want to merge the data with the same value of
"calleridnum":"0090000163",
"uid":"0090000163",
this is my first array:
[
{
"_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"
}
}
]
This is my second array
[
{
"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"
}
]
i want to merge the object with the same value of 0090000163
i want to get if calleridnum == uid
i want to get this output
"_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"
i do it in php . but i dont have much knowledge in javascript
this is my code in 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'];
}
}
}
asked Aug 21, 2015 at 6:26
user5245837user5245837
1 Answer 1
lets say you have two arrays of objects arr1
and arr2
You need to loop through both, compare the value calleridnum
from arr1
and uid
from arr2
if they are same then merge the object 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;
}
}
Cerbrus
73.2k19 gold badges137 silver badges151 bronze badges
answered Aug 21, 2015 at 7:10
-
1
arr1[i].merge
is not an existing function. Also, why didn't you just edit your other answer? Don't get me wrong, when you fix the merge function, this is the right answer.Cerbrus– Cerbrus2015年08月21日 07:11:57 +00:00Commented Aug 21, 2015 at 7:11 -
.merge() is a jquery functionVibhesh Kaul– Vibhesh Kaul2015年08月21日 07:13:24 +00:00Commented Aug 21, 2015 at 7:13
-
Yes, but it's not a function on a array or object. (
$.merge(a, b)
)Cerbrus– Cerbrus2015年08月21日 07:14:14 +00:00Commented Aug 21, 2015 at 7:14 -
used to merge properties of two objects!Vibhesh Kaul– Vibhesh Kaul2015年08月21日 07:14:39 +00:00Commented Aug 21, 2015 at 7:14
-
I know the function, but you're calling it incorrectly.Cerbrus– Cerbrus2015年08月21日 07:15:21 +00:00Commented Aug 21, 2015 at 7:15
lang-js
if (calleridnum == uid)
, if true -$.extend(array1, array2);