2017-03-02 1 views
0

Ich habe ein Array wie dieses in Javascript. So etwas wie dasWie sammle ich Wert in Javascript?

[ 
    { 
    "id": 1, 
    "facilities": [ 
     { 
     "id": 10, 
     "name": "Wifi", 
     "label": "Wifi" 
     }, 
     { 
     "id": 12, 
     "name": "Toll", 
     "label": "Toll" 
     } 
    ] 
    }, 
    { 
    "id": 2, 
    "facilities": [ 
     { 
     "id": 10, 
     "name": "Wifi", 
     "label": "Wifi" 
     }, 
     { 
     "id": 12, 
     "name": "Toll", 
     "label": "Toll" 
     }, 
     { 
     "id": 13, 
     "name": "Snack", 
     "label": "Snack" 
     } 
    ] 
    }, 
    { 
    "id": 3, 
    "facilities": [ 
     { 
     "id": 10, 
     "name": "Wifi", 
     "label": "Wifi" 
     }, 
     { 
     "id": 12, 
     "name": "Toll", 
     "label": "Toll" 
     }, 
     { 
     "id": 14, 
     "name": "Petrol", 
     "label": "Petrol" 
     } 
    ] 
    } 
] 

Ich möchte Daten sammeln und gruppieren Daten Einrichtungen des Arrays in Javascript, etwas wie dies.

"facilities": [ 
     { 
     "id": 10, 
     "name": "Wifi", 
     "label": "Wifi" 
     }, 
     { 
     "id": 12, 
     "name": "Toll", 
     "label": "Toll" 
     }, 
     { 
     "id": 13, 
     "name": "Snack", 
     "label": "Snack" 
     }, 
     { 
     "id": 14, 
     "name": "Petrol", 
     "label": "Petrol" 
     } 
    ] 

Also, im Grunde, Gruppe von Einrichtungen. Ich weiß einfach nicht, wie ich mit der Gruppierung ähnlicher Anlagenwerte umgehen soll.

Antwort

1

die Anlage id s Unter der Annahme, sind einzigartig:

const facilities = input.reduce((memo, entry) => { 
    entry.facilities.forEach((f) => { 
    if (!memo.some((m) => m.id === f.id)) { 
     memo.push(f) 
    } 
    }) 
    return memo 
}, []) 
0

Sie durch alle Reihen durchlaufen kann und (id, entity) Karte sammeln.
Indexkarte ermöglicht es uns nicht jedes Mal bereits gesammelte Entitäten zu durchsuchen.
Dann können Sie es in ein Array mit Objektschlüsselzuordnung konvertieren.

let input = [ 
 
    {"id": 1, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"} ] }, 
 
    {"id": 2, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 13, "name": "Snack", "label": "Snack"} ] }, 
 
    {"id": 3, "facilities": [{"id": 10, "name": "Wifi", "label": "Wifi"}, {"id": 12, "name": "Toll", "label": "Toll"}, {"id": 14, "name": "Petrol", "label": "Petrol"} ] } 
 
]; 
 

 
let index = input.reduce((res, row) => { 
 
    row.facilities.forEach(f => res[f.id] = f); 
 
    return res; 
 
}, {}); 
 

 
let result = Object.keys(index).map(id => index[id]); 
 
console.log({facilities: result});

0

Die Lösung mit Array.prototype.reduce() und Set Objekt:

var data = [{"id": 1,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"}]},{"id": 2,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 13,"name": "Snack","label": "Snack"}]},{"id": 3,"facilities": [{"id": 10,"name": "Wifi","label": "Wifi"},{"id": 12,"name": "Toll","label": "Toll"},{"id": 14,"name": "Petrol","label": "Petrol"}]} 
 
]; 
 

 
var ids = new Set(), 
 
    result = data.reduce(function (r, o) { 
 
     o.facilities.forEach(function(v) { // iterating through nested `facilities` 
 
      if (!ids.has(v.id)) r.facilities.push(v); 
 
      ids.add(v.id); // saving only items with unique `id` 
 
     }); 
 
     return r; 
 
    }, {facilities: []}); 
 

 
console.log(result);

0

Sie eine Set für das Markieren von eingefügten Objekten mit dem angegebenen nutzen könnten id.

var data = [{ id: 1, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }] }, { id: 2, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 13, name: "Snack", label: "Snack" }] }, { id: 3, facilities: [{ id: 10, name: "Wifi", label: "Wifi" }, { id: 12, name: "Toll", label: "Toll" }, { id: 14, name: "Petrol", label: "Petrol" }] }], 
 
    grouped = data.reduce(
 
     (s => (r, a) => (a.facilities.forEach(b => !s.has(b.id) && s.add(b.id) && r.push(b)), r))(new Set), 
 
     [] 
 
    ); 
 

 
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

Ein sehr einfacher und leicht Ansatz verstanden.

const data = [{ 
 
    "id": 1, 
 
    "facilities": [{ 
 
     "id": 10, 
 
     "name": "Wifi", 
 
     "label": "Wifi" 
 
     }, 
 
     { 
 
     "id": 12, 
 
     "name": "Toll", 
 
     "label": "Toll" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "id": 2, 
 
    "facilities": [{ 
 
     "id": 10, 
 
     "name": "Wifi", 
 
     "label": "Wifi" 
 
     }, 
 
     { 
 
     "id": 12, 
 
     "name": "Toll", 
 
     "label": "Toll" 
 
     }, 
 
     { 
 
     "id": 13, 
 
     "name": "Snack", 
 
     "label": "Snack" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "id": 2, 
 
    "facilities": [{ 
 
     "id": 10, 
 
     "name": "Wifi", 
 
     "label": "Wifi" 
 
     }, 
 
     { 
 
     "id": 12, 
 
     "name": "Toll", 
 
     "label": "Toll" 
 
     }, 
 
     { 
 
     "id": 14, 
 
     "name": "Petrol", 
 
     "label": "Petrol" 
 
     } 
 
    ] 
 
    } 
 
]; 
 

 
let o = {}; 
 
let result = []; 
 
data.forEach((d) => { 
 
    d.facilities.forEach((f) => { 
 
    o[f.id] = f; 
 
    }); 
 
}); 
 

 
for (let r in o) { 
 
    result.push(o[r]); 
 
} 
 

 
console.log(result);

0

const input = [ 
 
    { 
 
    "id": 1, 
 
    "facilities": [ 
 
     { 
 
     "id": 10, 
 
     "name": "Wifi", 
 
     "label": "Wifi" 
 
     }, 
 
     { 
 
     "id": 12, 
 
     "name": "Toll", 
 
     "label": "Toll" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "id": 2, 
 
    "facilities": [ 
 
     { 
 
     "id": 10, 
 
     "name": "Wifi", 
 
     "label": "Wifi" 
 
     }, 
 
     { 
 
     "id": 12, 
 
     "name": "Toll", 
 
     "label": "Toll" 
 
     }, 
 
     { 
 
     "id": 13, 
 
     "name": "Snack", 
 
     "label": "Snack" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "id": 3, 
 
    "facilities": [ 
 
     { 
 
     "id": 10, 
 
     "name": "Wifi", 
 
     "label": "Wifi" 
 
     }, 
 
     { 
 
     "id": 12, 
 
     "name": "Toll", 
 
     "label": "Toll" 
 
     }, 
 
     { 
 
     "id": 14, 
 
     "name": "Petrol", 
 
     "label": "Petrol" 
 
     } 
 
    ] 
 
    } 
 
] 
 

 
const result = [] 
 
const idx = [] 
 

 
for (const item of input) { 
 
    for (const facilityItem of item.facilities) { 
 
    if (!idx.includes(facilityItem.id)) { 
 
     idx.push(facilityItem.id) 
 
     result.push(facilityItem) 
 
    } 
 
    } 
 
} 
 

 
console.log(result)

Verwandte Themen