Reputation: 1449
var obj2 = [{
"name": "4134",
"calls": [
]
}]
var obj3 = [{ Channel: 'SIP/4134-0004462a',
State: 'Up',
Accountcode: '7013658596'},
{ Channel: 'SIP/4334-sa',
State: 'Up',
Accountcode: '07717754702',
}]
var channelArr = [];
const Channels = obj3.reduce((acc, curVal) => {
obj2.forEach((item)=>{
if(curVal.Channel.includes('SIP') && item.name == curVal.Channel.slice(4,8).toString()){
item.calls.push({'MobileNo':curVal.Accountcode,'Status': curVal.State})
}
})
return obj2;
}, [])
console.log(obj2);
The output of the above code is
[
{
"name": "4134",
"calls": [
{
"MobileNo": "7013658596",
"Status": "Up"
}
]
}
]
My code takes "name": "4134"
from obj2 and Channel: 'SIP/4134-0004462a
from obj3. It then checks if 4134 from obj2 exists in the value 'SIP/4134-0004462a'
from obj3 using the code curVal.Channel.includes('SIP') && item.name == curVal.Channel.slice(4,8).toString()
. If the value exists then it pushes the MobileNo
and AccountCode
from that particular object and pushes it into the calls array of obj2.
Since Channel: 'SIP/4134-0004462a
is very predictable here as if the value contains SIP
and slice(4,8)
position then 4134
matches perfectly. But what if instead of 4134
I have to match a value like "name" : "4134556"
? In this case it won't work as 4134556
won't match with 'SIP/4134556-0004462a
if I use slice(4,8)
on obj3.
I want a solution such that it takes the value of name key from obj2 and takes the value of Channel key from obj3 and check if the value from obj2 exists in Channel value starting from position 4 (i.e after SIP/ ) I don't want to set a limit like splice(4,8)
, instead it should be something dynamic which can match any digit of name with the Channel starting from position 4 in the Channel value (expecting something like splice(start from 4th position and match the next digits for a match)
). This will let me match the numbers perfectly even if they have any number of digits.
What I tried as a temporary solution :
if(curVal.Channel.includes('SIP') && curVal.Channel.includes(item.name))
but this gives me wrong result at Channel : SIP/3422-0413462a
from obj3 will also match with name : 4134
from obj2 as it contains the number 4134 at some other position and this is not the desired result.
How do I do it such that name from obj2 is matched with Channel of obj3 starting from the 4th position i.e after SIP/
and check if there's a match?
Upvotes: 0
Views: 53
Reputation: 97130
You can use String.prototype.startsWith(searchString, position)
:
console.log('SIP/4134556-0004462a'.startsWith('4134556', 4)); // true
The position
parameter allows to specify where in the haystack you want to search for the needle (in your case position 4
).
This might give you another issue, as this will also evaluate to true:
'SIP/4134556-0004462a'.startsWith('4134', 4); // true
If this is not what you want (i.e. you want the value to be followed by the -
separator), you have to add the separator to your search string:
'SIP/4134556-0004462a'.startsWith('4134' + '-', 4) // false
Upvotes: 3
Reputation: 16049
Get Sub string of Channel
from position 4
to end and check name is exist in that substring or not
var channel = "SIP/4134123-0004462a"; //your channel name
console.log(channel.substring(4).includes("4134123")); //your name
Upvotes: 1
Reputation: 37755
You can use name length as the second parameter to slice. you don't need regex here mate :)
Something like this
curVal.Channel.slice(4,4+item.name.length)
^^^^^^^^^^^^^^^^^^
var obj2 = [{
"name": "4134123",
"calls": [
]
}]
var obj3 = [{ Channel: 'SIP/4134123-0004462a',
State: 'Up',
Accountcode: '7013658596'},
{ Channel: 'SIP/4334-sa',
State: 'Up',
Accountcode: '07717754702',
}]
var channelArr = [];
const Channels = obj3.reduce((acc, curVal) => {
obj2.forEach((item)=>{
if(curVal.Channel.includes('SIP') && item.name == curVal.Channel.slice(4,4+item.name.length).toString()){
item.calls.push({'MobileNo':curVal.Accountcode,'Status': curVal.State})
}
})
return obj2;
}, [])
console.log(obj2);
Upvotes: 1