2017-10-09 121 views
0

我有這個對象的數組:搜索嵌套TS

const equipmentSchema = new Schema({ 
    country: { type: String, required: true }, 
    uf: { type: String, required: true }, 
    state: { type: String, required: true }, 
    city: { type: String, required: true }, 
    cp: { type: String }, 
    alias: { type: String }, 
    address: { type: String }, 
    location: { type: String }, 
    vendor: { type: String }, 
    hostname: { type: String, required: true, uppercase: true }, 
    type: { type: String, required: true }, 
    model: { type: String, required: true }, 
    cards: [{ 
     model: { type: String, required: true }, 
     slot: {type: String, required: true }, 
     typePort: { type: String, required: true }, 
     ports: [{ 
      numberPort: { type: Number, required: true }, 
      connector: { type: String }, 
      status: { type: String, required: true }, 
      speedCircuit: { type: String }, 
      serviceType: { type: String }, 
      network: { type: String }, 
      connectedTo: { type: String }, 
      customerName: { type: String }, 
      addressCustomer: { type: String }, 
      lelisID: { type: String }, 
      requester: { type: String }, 
      dateRequester: { type: Date }, 
      carrier: { type: String } 
     }] 
    }] 

我沒有任何想法我如何通過「客戶名稱」搜索和retrive的所有對象。 (我使用Angular2)

感謝所有!

回答

0

array.filter(o => !!o.cards.find(c => !!c.ports.find(p => p.customerName === "Marek")))

要掃描嵌套數組,你可以使用找到將返回第一個匹配的元素或未定義,如果有你的數組中不匹配。濾料函數將返回布爾所以要提高可讀性我們投對象或undefined使用爲布爾值!。因爲我們在外部數組上進行篩選,所以將返回整個對象。

+0

好極了!謝謝。它完美的作品。 –