搜索数组对象嵌套ts

时间:2017-10-09 15:56:59

标签: arrays typescript filter

我有这个对象的数组:

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 }
       }]
   }]

我不知道如何通过"客户名称"并检索所有对象。 (我使用的是Angular2)

非常感谢!!!

1 个答案:

答案 0 :(得分:1)

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

要扫描嵌套数组,您可以使用查找,如果您的数组中没有匹配项,则会返回第一个匹配元素或未定义。 Filter期望函数将返回boolean,以便提高可读性,我们使用 !! 将对象或undefined转换为boolean。因为我们在外部数组上进行过滤,所以将返回整个对象。