1

I have arrays of this object:

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

I don't have any idea how can i search by "Customer name" and retrive all object. (I'm using Angular2)

Thanks for all!!!

1 Answer 1

2

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

To scan nested arrays you can use find which will return first matching element or undefined if there's no match in your array. Filter expects function which will return boolean so to improve readability we cast object or undefined to boolean using !!. Because we're filtering on outer array, entire objects will be returned.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.