Created
October 24, 2018 09:29
-
-
Save harrytwright/1edf396dbf4829ce5d5784cb8928be8d to your computer and use it in GitHub Desktop.
Change array value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let AddressSchema = mongoose.Schema({ | |
formattedAddress: String, | |
components: [ | |
{ | |
longName: String, | |
shortName: String, | |
types: [String] | |
} | |
] | |
}); | |
AddressSchema.methods.changeComponents = function (comp) { | |
function findFirstComponent(element) { | |
return element.types === comp.types | |
} | |
const index = this.components.findIndex(findFirstComponent) || this.components.length; | |
this.components[index] = comp; | |
return this.save(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment