2017-10-09 117 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

好极了!谢谢。它完美的作品。 –