Reputation: 1449
Here is my code
const obj1 = [{ext: '1287',ip: '(Unspecified)',queue: []}];
const obj2 = [{Queue: '222',Members: [{"ext":"1287"},{"ext":"4118"}],Callers: []},{Queue: '111',Members: [{"ext":"4131"},{"ext":"1287"},{"ext":"4138"}],Callers: []}];
const hasExt = ext => o2 => o2.Members.some(m => m.ext === ext)
const result = obj1.map(o1 => {
const newQueue = obj2
.filter(hasExt(o1.ext))
.map(m => m.Queue);
return { ...o1, queue: [...o1.queue, ...newQueue] };
})
console.log(result);
Here is my js fiddle https://jsfiddle.net/pravinnavle/myzsvbLr/1/ In the above part I'm comparing obj1 and obj2. If the value of ext key from obj1 matches the ext key of nested Members array of obj2 then the value of Queue key of obj2 get's pushed to a queue array in obj1. The output is as follow:
[
{
"ext": "1287",
"ip": "(Unspecified)",
"queue": [
"222",
"111"
]
}
]
Now I have a third array obj3 like this
const obj3 = [{ ext: '1287',Prio: '1',State: 'Up'},{ ext: '1184',Prio: '1',State: 'Down'}]
I want to compare obj1 and obj3. If the value of ext key from obj1 matches the value of ext key in obj3 then the Prio and State keys of obj3 should be pushed to a calls array in obj1 and the final array should be like as shown below.
[
{
"ext": "1287",
"ip": "(Unspecified)",
"queue": [
"222",
"111"
],
"calls": [
{
"Prio" : 1,
"State" : "Up",
}
],
}
]
I'm having trouble to compare obj1 with obj3 and output it in the result constant that is present in the code at the beginning. How do I do that?
Upvotes: 0
Views: 242
Reputation: 1917
You can try something like that.
const obj2 = [{
"ext": "1287",
"ip": "(Unspecified)",
"queue": [
"222",
"111"
]
}
];
const obj3 = [{ ext: '1287',Prio: '1',State: 'Up'},{ ext: '1184',Prio: '1',State: 'Down'}];
const func = (obj1, obj2) => {
obj1.forEach((a) =>
obj2.forEach((b) => {
if (b.ext === a.ext) a.calls = (a.calls || []).concat(Object.assign({}, { Prio: b.Prio, State: b.State }));
})
);
};
func(obj2, obj3);
console.log(obj2);
Upvotes: 1