2016-06-08 9 views
1

Ich muss zwei Arrays vergleichen und Elemente zu einem Array schieben, wenn keines der Objekte in diesem Array mit dem zweiten Array übereinstimmt.Benötigen Sie Hilfe beim Durchlaufen von 2 verschachtelten Arrays in Javascript/AngularJS und tun Vergleich

Bitte meine JSON die Sie interessieren könnten:

Array 1:

"[{ 
"carrierName":"A", 
"id":7, 
"active":true 
"subList":[{ 
"active":false, 
"carrierServiceId":"19", 
"locationPrimaryContactName":"ABC 4", 
"locationPrimaryContactNumber":"1111", 
"locationStateCode":"NJ", 
"locationZipcode":"56324", 
"name":"A 4"} 
, 
{ 
"active":true, 
"carrierServiceId":"20", 
"locationPrimaryContactName":"ABC 1", 
"locationPrimaryContactNumber":"1111", 
"locationStateCode":"NJ", 
"locationZipcode":"56324", 
"name":"A 1" 
}, 
{ 
"active":true, 
"carrierServiceId":"21", 
"locationPrimaryContactName":"ABC 2", 
"locationPrimaryContactNumber":"1111", 
"locationStateCode":"NJ", 
"locationZipcode":"56324", 
"name":"A 2"}, 
{ 
"active":true, 
"carrierServiceId":"22", 
"id":1001, 
"locationPrimaryContactName":"ABC 3", 
"locationPrimaryContactNumber":"1111", 
"locationStateCode":"NJ", 
"locationZipcode":"56324", 
"name":"A 3" 
} 
] 
} 
]" 

Array 2:

"[{"subList":[ 
    { 
     "active":true, 
     "code":"20", 
     "id":0, 
     "locationPrimaryContactName":"ABC 1", 
     "locationPrimaryContactNumber":"1111", 
     "name":"A 1", 
    }, 
    { 
     "active":true, 
     "code":"21", 
     "id":0, 
     "locationPrimaryContactName":"ABC 2", 
     "locationPrimaryContactNumber":"1111", 
     "name":"A 2", 
    }, 
    { 
     "active":true, 
     "code":"22", 
     "id":0, 
     "locationPrimaryContactName":"ABC 3", 
     "locationPrimaryContactNumber":"1111", 
     "name":"A 3", 
    }, 
    { 
     "active":true, 
     "code":"19", 
     "id":0, 
     "locationPrimaryContactName":"ABC 4", 
     "locationPrimaryContactNumber":"1111", 
     "name":"A 4", 
    } 
], 
"active":false, 
"id":7, 
"name":"A", 
}, 
{ 
"subList":[ 
    { 
     "active":true, 
     "code":"7", 
     "id":0, 
     "locationPrimaryContactName":"DEF 1", 
     "locationPrimaryContactNumber":"2222", 
     "name":"B 1", 
    }, 
    { 
     "active":true, 
     "code":"8", 
     "id":0, 
     "locationPrimaryContactName":"DEF 2", 
     "locationPrimaryContactNumber":"2222", 
     "name":"B 2", 
    }, 
    { 
     "active":true, 
     "code":"9", 
     "id":0, 
     "locationPrimaryContactName":"DEF 3", 
     "locationPrimaryContactNumber":"2222", 
     "name":"B 3", 
    } 
], 
"active":false, 
"id":8, 
"name":"B", 
}, 
{ 
"subList":[ 
    { 
     "active":true, 
     "code":"10", 
     "locationPrimaryContactName":"GHI 1", 
     "locationPrimaryContactNumber":"3333", 
     "name":"C 1", 
    } 
], 
"active":false, 
"id":9, 
"name":"C", 
}, 
{ 
"subList":[ 

], 
"active":false, 
"id":10, 
"name":"D", 
} 
]" 

Hier, wenn Sie sehen, die gemeinsamen Felder sind id, sublist.carrierServiceId in Array 1 & subList.code in Array 2. Meine Anforderung ist, dass ich Array 1 und Array 2 durchlaufen muss und die Elemente von Array 2 zu Array 1 hinzufügen, Wenn es nicht existiert, Array 1. Wenn Sie hinzufügen, müssen Sie das Array "subList" auch für eine bestimmte "id" in Array 2 überprüfen und wenn diese in Array 1 vorhanden sind, sollten sie auch nicht hinzugefügt werden.

Fragen Sie einen Experten zu diesem Thema. Bitte lassen Sie mich wissen, wenn die Abfrage mehr Klarheit benötigt.

+0

bitte das gewünschte Ergebnis hinzuzufügen und den Code, den Sie versucht. –

+0

zum Beispiel, was soll mit 'code = 7' geschehen? –

Antwort

0

Wenn ich es richtig verstehe, müssen Sie alle Elemente von Array 2 in Array 1 an der richtigen Stelle erhalten.

Dies funktioniert mit einem Verweis auf die angegebenen Objekte und erstellt bei Bedarf ein neues Objekt.

var array1 = [{ carrierName: "A", id: 7, active: true, subList: [{ active: false, carrierServiceId: "19", locationPrimaryContactName: "ABC 4", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 4" }, { active: true, carrierServiceId: "20", locationPrimaryContactName: "ABC 1", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 1" }, { active: true, carrierServiceId: "21", locationPrimaryContactName: "ABC 2", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 2" }, { active: true, carrierServiceId: "22", id: 1001, locationPrimaryContactName: "ABC 3", locationPrimaryContactNumber: "1111", locationStateCode: "NJ", locationZipcode: "56324", name: "A 3" }] }], 
 
    array2 = [{ subList: [{ active: true, code: "20", id: 0, locationPrimaryContactName: "ABC 1", locationPrimaryContactNumber: "1111", name: "A 1", }, { active: true, code: "21", id: 0, locationPrimaryContactName: "ABC 2", locationPrimaryContactNumber: "1111", name: "A 2", }, { active: true, code: "22", id: 0, locationPrimaryContactName: "ABC 3", locationPrimaryContactNumber: "1111", name: "A 3", }, { active: true, code: "19", id: 0, locationPrimaryContactName: "ABC 4", locationPrimaryContactNumber: "1111", name: "A 4", }], active: false, id: 7, name: "A", }, { subList: [{ active: true, code: "7", id: 0, locationPrimaryContactName: "DEF 1", locationPrimaryContactNumber: "2222", name: "B 1", }, { active: true, code: "8", id: 0, locationPrimaryContactName: "DEF 2", locationPrimaryContactNumber: "2222", name: "B 2", }, { active: true, code: "9", id: 0, locationPrimaryContactName: "DEF 3", locationPrimaryContactNumber: "2222", name: "B 3", }], active: false, id: 8, name: "B", }, { subList: [{ active: true, code: "10", locationPrimaryContactName: "GHI 1", locationPrimaryContactNumber: "3333", name: "C 1", }], active: false, id: 9, name: "C", }, { subList: [], active: false, id: 10, name: "D" }], 
 
    reference = {}; 
 

 
array1.forEach(function (a) { 
 
    if (!reference[a.id]) { 
 
     reference[a.id] = { data: a, children: {} }; 
 
    } 
 
    a.subList.forEach(function (b) { 
 
     reference[a.id].children[b.carrierServiceId] = b; 
 
    }); 
 
}); 
 

 
array2.forEach(function (a) { 
 
    if (!reference[a.id]) { 
 
     reference[a.id] = { data: { carrierName: a.name, id: a.id, active: a.active, subList: [] }, children: {} }; 
 
     array1.push(reference[a.id].data); 
 
    } 
 
    a.subList.forEach(function (b) { 
 
     if (!reference[a.id].children[b.code]) { 
 
      reference[a.id].children[b.code] = { 
 
       active: b.active, 
 
       carrierServiceId: b.code, 
 
       locationPrimaryContactName: b.locationPrimaryContactName, 
 
       locationPrimaryContactNumber: b.locationPrimaryContactNumber, 
 
       // locationStateCode: b.locationPrimaryContactNumber, 
 
       // locationZipcode: b.locationZipcode, 
 
       name: b.name 
 
      }; 
 
      reference[a.id].data.subList.push(reference[a.id].children[b.code]); 
 
     } 
 
    }); 
 
}); 
 

 
console.log(array1);

+0

Nina, vielen Dank !!!! Genau das, was ich wollte. –

Verwandte Themen