问题
How do I make a pattern like this? I can't code that because i'm new to this.
This is my code
array = 1,2,3,4,5
const note = new notedModel ({
_id: array,
note: args[1],
});
Also using other modules if you ask.
回答1:
I hope it could be helpful
const array = [1,2,3,4,5];
const notes = array.map(element => new notedModel({
_id: element,
note: args[1], // or maybe args[element] if you need to select arg by array's element
}));
P.S. You could read answers here to be careful with ids: How do you implement an auto-incrementing primary ID in MongoDB?
回答2:
I hope this helps
class NotedModel{
constructor(id, arg){
this._id = id
this._note = arg
}
}
var arr = [1, 2, 3, 4, 5]
var note = [];
for(i = 0; i < arr.length; i++){
note.push(new NotedModel(arr[i], i));
}
console.log(note);
来源:https://stackoverflow.com/questions/62547933/how-can-i-make-a-number-id-pattern-1-2-3-4-in-mongoose