1// IN THIS EXAMPLE WE HAVE GROUP INSERTS - DATA TO BE ADDED TO DB
2// TO INSERT INTO DB EACH GROUP NAME MUST BE THE MATCHING ID
3// CONATCT NAME MUST ALSO BE MATCHING ID
4// THE CODE BELOW SHOWS HOW TO TRAVERSE THROGUH TWO ARRAYS AND COMPARE / ADD VALES
5
6const groupInserts = [ { groups: [ 'Chess', 'Science', 'German' ], name: 'Ian Smith' },
7 { groups: [ 'Swimming' ], name: 'Jenny Smith' },
8 { groups: [ 'Tennis' ], name: 'Susan Jones' },
9 { groups: [ 'Coding', 'Science' ], name: 'Judy Davis' } ]
10
11const nameAndIdsFromDB = [ { name: 'Parent_1', id: 1 },
12{ name: 'Parent_2', id: 2 },
13{ name: 'Ian Smith', id: 99 },
14{ name: 'Jenny Smith', id: 100 },
15{ name: 'Susan Jones', id: 101 },
16{ name: 'Judy Davis', id: 102 } ]
17
18const groupnameAndIdsFromDB = [ { name: 'Debugging', id: 1 },
19{ name: 'React', id: 2 },
20{ name: 'New record with no people', id: 3 },
21{ name: 'New record with people', id: 4 },
22{ name: 'Chess', id: 12 },
23{ name: 'Science', id: 13 },
24{ name: 'German', id: 14 },
25{ name: 'Swimming', id: 15 },
26{ name: 'Tennis', id: 16 },
27{ name: 'Coding', id: 17 } ]
28
29let groupNamesWithIds = [];
30groupInserts.forEach(data => {
31 if (data.groups) {
32 data.groups.map(e => {
33 let obj = {};
34 obj.group = e;
35 obj.name = data.name
36 groupNamesWithIds.push(obj);
37 })
38 }
39})
40
41
42let addedMessageGroupIds = [];
43groupNamesWithIds.forEach(data => {
44 groupnameAndIdsFromDB.map(e => {
45 if (e.name === data.group) {
46 addedMessageGroupIds.push({group: data.group, name: data.name, messages_individual_groups_id: e.id})
47 }
48 })
49})
50
51let addedContactIds = [];
52addedMessageGroupIds.forEach(data => {
53 nameAndIdsFromDB.map(e => {
54 if (e.name === data.name) {
55 addedContactIds.push({messages_individual_groups_id: data.messages_individual_groups_id, contact_id: e.id, created_at: Date.now()})
56 }
57 })
58})
1a = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal"}, { value:"a63a6f77-c637-454e-abf2-dfb9b543af6c", display:"Ryan"}]
2b = [{ value:"4a55eff3-1e0d-4a81-9105-3ddd7521d642", display:"Jamsheer", $$hashKey:"008"}, { value:"644838b3-604d-4899-8b78-09e4799f586f", display:"Muhammed", $$hashKey:"009"}, { value:"b6ee537a-375c-45bd-b9d4-4dd84a75041d", display:"Ravi", $$hashKey:"00A"}, { value:"e97339e1-939d-47ab-974c-1b68c9cfb536", display:"Ajmal", $$hashKey:"00B"}]
3
4function comparer(otherArray){
5 return function(current){
6 return otherArray.filter(function(other){
7 return other.value == current.value && other.display == current.display
8 }).length == 0;
9 }
10}
11
12var onlyInA = a.filter(comparer(b));
13var onlyInB = b.filter(comparer(a));
14
15result = onlyInA.concat(onlyInB);
16
17console.log(result);