0

I have constructed objects as follow:

Object 1

[ { 
   ext: '1287',
   ip: '(Unspecified)',
   queue: [ ]
 } ]

Object 2

 [ { Queue: '222',
    Members:
     [ {"ext":"1287"},
       {"ext":"4118"} ],
    Callers: [] },

  { Queue: '111',
    Members:
     [ {"ext":"4131"},
       {"ext":"1287"},
       {"ext":"4138"}
     ],
    Callers: [] }]

I want to compare Object 1 and Object 2. If the value of ext key from Object 1 exists in the nested Members object of Object 2 then the value of Queue should be pushed to a queue array and the final object should be like as shown below.

Final Object that I want

   [{ ext: '1287',
   ip: '(Unspecified)',
   queue: [222, 111 ] }]

I need some hints regarding how a nested object like this is compared using lodash.

3
  • 3
    '{"ext":"4131"}' is a string in the question. Is it a typo? Commented Sep 12, 2018 at 11:39
  • 3
    a) There is no such thing as JSON objects. b) You show arrays, not objects. c) Your JSON is invalid, property names b must be quoted in JSON. Commented Sep 12, 2018 at 11:40
  • '{"ext":"4131"}', '{"ext":"1287"}', '{"ext":"4138"}' object 2 is members are array of string OR each behaving as an individual object Commented Sep 12, 2018 at 11:42

2 Answers 2

3

You can try following using Array.forEach and Array.some

let obj1 = [{ext: '1287',ip: '(Unspecified)',queue: []}];
let obj2 = [{Queue: '222',Members: [{"ext":"1287"},{"ext":"4118"}],Callers: []},{Queue: '111',Members: [{"ext":"4131"},{"ext":"1287"},{"ext":"4138"}],Callers: []}];

obj1.forEach(o => {
  obj2.forEach(v => {
    let exists = v.Members.some(m => m.ext === o.ext);
    if (exists) o.queue.push(v.Queue);
  });
});

console.log(obj1);

Improvement

You can improve the performance by first creating a map of obj1 with ext as key and object as value. Use Array.reduce and Object.assign

let obj1 = [{ext: '1287',ip: '(Unspecified)',queue: []}];
let obj2 = [{Queue: '222',Members: [{"ext":"1287"},{"ext":"4118"}],Callers: []},{Queue: '111',Members: [{"ext":"4131"},{"ext":"1287"},{"ext":"4138"}],Callers: []}];

let map = obj1.reduce((a, c) => Object.assign(a, {[c.ext] : c}), new Map());

obj2.forEach(v => {
  v.Members.forEach(m => {
    if(map[m.ext]) map[m.ext].queue.push(v.Queue);
  });
});

console.log(obj1);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Nikhil. This solution is perfect. Also, I corrected my question
@NAVLEPRAVIN - Glad to help you!
1

Solution without mutations:

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);

8 Comments

Hey this solution is also very helpful. Thank you
Hey how do I do it if I want the array like this [ { "ext": "1287", "ip": "(Unspecified)", "queue": [ {"number" : "222"}, {"number" : "111"} ] } ]
@pravinnavle change .map(m => m.Queue) to .map(m => ({number: m.Queue}))
Thanks a lot, it worked. Sorry I'm accepting more and more help.
@pravinnavle .map(m => ({number: m.Queue, name: m.Members.find(m => m.ext === o1.ext).name})) best resource for web platform is developer.mozilla.org. If you want to go deep with js -> github.com/getify/You-Dont-Know-JS. or videos at frontendmasters.com (all courses are free to watch this week)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.