2

Here is my code :

var obj2 = [{
  "name": "4134",
  "calls": [

  ]
}]

    var obj3 = [{ Channel: 'SIP/4134-0004462a',
        Accountcode: '7013658596'},
      { Channel: 'DAHDI-il-4134-sa',
        Accountcode: '07717754702',
      }]

var func = (obj2, obj3) => {
    obj2.forEach((a) =>
      obj3.forEach((b) => {
        if (b.Channel.includes(a.name)) a.calls = (a.calls || []).concat(Object.assign({}, { MobileNo: b.Accountcode}));

      })
    );
};

func(obj2, obj3);
console.log(JSON.stringify(obj2));

Link to my JS-Fiddle Here is my current output:

[
  {
    "name": "4134",
    "calls": [
      {
        "MobileNo": "7013658596"
      },
      {
        "MobileNo": "07717754702"
      }
    ]
  }
]

In the above code I have two objects. In obj2 the value of the name key is 4134. In my code I'm checking if this 4134 exists in the value of the Channel key in obj3 by using if (b.Channel.includes(a.name)). I have used the includes function here. So if 4134 exists in the value of Channel in obj3 then the Accountcode is pushed into the calls array of obj2 when there's a match. As you can see one of Channel is SIP/4134-0004462a and the other one is DAHDI-il-4134-sa. Both of them contain 4134 but I only want to compare the values where the Channel contains the text SIP in it. As you can see in the above output that the Accountcode from both the objects in obj3 are pushed to calls array in obj2 as both the Channels contain 4134. This is not what I want.

Here is the desired output that I want :

[
  {
    "name": "4134",
    "calls": [
      {
        "MobileNo": "7013658596"
      }
    ]
  }
]

In the above-desired output, 4134 is matched only with the Channel containing the text SIP in it. How do I do it using the includes function?

Prashant Gupta
  • 788
  • 8
  • 26
node_man
  • 1,359
  • 4
  • 23
  • 50
  • Possible duplicate of [multiple conditions for JavaScript .includes() method](https://stackoverflow.com/questions/37896484/multiple-conditions-for-javascript-includes-method) – Nick Parsons Sep 25 '18 at 09:55

2 Answers2

1

Change if (b.Channel.includes(a.name))... to if(b.Channel.includes(a.name) && b.Channel.match('SIP'))...

adam.k
  • 622
  • 3
  • 15
  • but the value of the name key from obj2 should also be there in Channel key if the pattern contains SIP. There are many objects in my array. So the Channel should contain SIP as well the value of the name key for obj2 – node_man Sep 25 '18 at 09:52
  • 1
    I edited my answer. Perhaps this is what you are looking for. – adam.k Sep 25 '18 at 10:03
  • thank you. this will also work. I have answered my question below – node_man Sep 25 '18 at 10:04
0

I solved the problem by using one more condition in the if statement like this

if (b.Channel.includes('SIP') && b.Channel.includes(a.name))

Final Code

var obj2 = [{
  "name": "4134",
  "calls": [

  ]
}]

    var obj3 = [{ Channel: 'SIP/4134-0004462a',
        Accountcode: '7013658596'},
      { Channel: 'DAHDI-il-4134-sa',
        Accountcode: '07717754702',
      }]

var func = (obj2, obj3) => {
    obj2.forEach((a) =>
      obj3.forEach((b) => {
        if (b.Channel.includes('SIP') && b.Channel.includes(a.name)) a.calls = (a.calls || []).concat(Object.assign({}, { MobileNo: b.Accountcode}));

      })
    );
};

func(obj2, obj3);
console.log(JSON.stringify(obj2));

Now I'm getting the desired output as well.

[
  {
    "name": "4134",
    "calls": [
      {
        "MobileNo": "7013658596"
      }
    ]
  }
]

Thank you community for giving your time.

node_man
  • 1,359
  • 4
  • 23
  • 50