鉴于:
array = ["1289", "12 apples", "12546", "123"]
如何删除阵列中的所有元素,并删除“12”?我正在寻找的答案应该适用,即使数组有数百个元素。
答案 0 :(得分:0)
只需将map
与gsub
(或sub
一起使用,当12
只出现在字符串中一次时):
array = [ "1289", "12 apples", "12546", "123"]
array.map { |word| word.gsub('12', '') }
#=> ["89", " apples", "5463", "3"]
答案 1 :(得分:0)
您可以使用以下任何一项。
import mongoose from 'mongoose';
var Schema=mongoose.Schema;
var db= mongoose.connect('mongodb://127.0.0.1:27017/student');
mongoose.connect('connected',function(){
console.log("database connected successfully")
});
var userSchema=new Schema({
Name:{type:String,required:true,unique:true},
Age:{type:Number,required:true}
},{collection :'studentcollection'});
var User = mongoose.model('User',userSchema);
function createStudent(name,age){
var list=new User({
Name:name,
Age: age
});
list.save(function() {
User.find({}, function(err, studentcollection) {
if (err) throw err;
console.log(studentcollection);
console.log("successful")
});
});
}
exports.createStudent = createStudent;
解决方案1 (使用string#tr方法)
array = [ "1289", "12 apples", "1254612", "123"]
解决方案2 (使用string#gsub方法)
array.each{|string| puts string.tr("/12(\s+)?/", '')}
## Output
"89"
"apples"
"546"
"3"
答案 2 :(得分:-3)
如果你想“就地”这样做我会的
array = ["1289", "12 apples", "12546", "123"]
array.each{ |s| s['12'] = '' }