我有一个对象数组
people = [
{id: "1", name: "abc", gender: "m", age:"15" },
{id: "2", name: "a", gender: "m", age:"25" },
{id: "3", name: "efg", gender: "f", age:"5" },
{id: "4", name: "hjk", gender: "m", age:"35" },
{id: "5", name: "ikly", gender: "m", age:"41" },
{id: "6", name: "ert", gender: "f", age:" 30" },
{id: "7", name: "qwe", gender: "f", age:" 31" },
{id: "8", name: "bdd", gender: "m", age:" 78" },
]
Run Code Online (Sandbox Code Playgroud)
我有另一个数组:
id_filter = [1,4,5,8]
Run Code Online (Sandbox Code Playgroud)
如果id匹配数组中的任何元素,我想过滤人的id_filter …
我希望在调用第二个“then”时不执行第三个“then”。然而,即使承诺被拒绝(第二个“then”被调用)并且代码返回“rejected”然后“undefined”,它仍然调用第三个“then”。如何不运行第三个“then”,这样“undefined”就不会出现?
var FirstPromise = function() {
let promiseme = new Promise(function(res, rej) {
if ('a' == 'b') {
res(1);
} else {
rej('rejected');
}
})
return promiseme;
};
function succeddcallback(msg) {
return msg * 2;
}
function errorcallback(msg) {
console.log(msg);
}
FirstPromise()
.then(succeddcallback, null)
.then(null, errorcallback)
.then(function(succedded) {
console.log(succedded);
}, function(failed) {
console.log(failed);
})
Run Code Online (Sandbox Code Playgroud) 我想找到一个字符串的所有组合,保持顺序,但任何长度。例如:
string_combinations("wxyz")
# => ['w', 'wx', 'wxy', 'wxyz', 'wxz', 'wy', 'wyz', 'wz', 'x', 'xy', 'xyz', 'xz', 'y', 'yz', 'z']
Run Code Online (Sandbox Code Playgroud)
我希望您只能使用循环并避免使用 ruby 方法,就像#combination我试图找到最干净的方法来实现它,如果我在另一种语言中遇到它。
有没有办法在小于 O(n^3) 的时间内做到这一点?我最初的想法是这样的:
def string_combinations(str)
result = []
(0...str.length).each do |i|
result << str[i]
((i+1)...str.length).each do |j|
result << str[i] + str[j]
((j+1)...str.length).each do |k|
result << str[i] + str[j..k]
# Still not covering everything.
end
end
end
result
end
Run Code Online (Sandbox Code Playgroud)