Skip to content

Instantly share code, notes, and snippets.

@harrytwright
Created October 24, 2018 09:29
Show Gist options
  • Save harrytwright/1edf396dbf4829ce5d5784cb8928be8d to your computer and use it in GitHub Desktop.
Save harrytwright/1edf396dbf4829ce5d5784cb8928be8d to your computer and use it in GitHub Desktop.
Change array value
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