我有一个JavaScript数组,如:
[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]]
Run Code Online (Sandbox Code Playgroud)
我将如何将单独的内部数组合并为:
["$6", "$12", "$25", ...]
Run Code Online (Sandbox Code Playgroud) function foo(a) {
if (/*some condition*/) {
// perform task 1
// perform task 3
}
else {
// perform task 2
// perform task 3
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个功能,其结构类似于上面的.我想将任务3抽象为一个函数,bar()但是我希望将此函数的访问权限限制在范围内foo(a).
为了达到我想要的目的,改为以下是否正确?
function foo(a) {
function bar() {
// perform task 3
}
if (/*some condition*/) {
// perform task 1
bar();
}
else {
// perform task 2
bar();
}
}
Run Code Online (Sandbox Code Playgroud)
如果以上是正确的,bar()每次foo(a)调用时都会重新定义吗?(担心这里浪费cpu资源)
我们知道,[[0, 1], [2, 3], [4, 5]]使用该方法展平数组reduce()
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
});
Run Code Online (Sandbox Code Playgroud)
因此,如何扁平化这个数组[[[0], [1]], [[2], [3]], [[4], [5]]]来[0, 1, 2, 3, 4, 5]?