我被告知不要for...in在JavaScript中使用数组.为什么不?
async在await循环中使用是否有任何问题?我正在尝试循环遍历文件数组和forEach每个文件的内容.
import fs from 'fs-promise'
async function printFiles () {
const files = await getFilePaths() // Assume this works fine
files.forEach(async (file) => {
const contents = await fs.readFile(file, 'utf8')
console.log(contents)
})
}
printFiles()
Run Code Online (Sandbox Code Playgroud)
这段代码确实有效,但这可能会出错吗?我有人告诉我你不应该使用await这样的高阶函数,所以我只是想问一下这是否有任何问题.
在javascript中实现数组交叉的最简单,无库的代码是什么?我想写
intersection([1,2,3], [2,3,4,5])
Run Code Online (Sandbox Code Playgroud)
得到
[2, 3]
Run Code Online (Sandbox Code Playgroud) 所以我想说我已经在Array类中添加了一些原型方法:
Array.prototype.containsKey = function(obj) {
for(var key in this)
if (key == obj) return true;
return false;
}
Array.prototype.containsValue = function(obj) {
for(var key in this)
if (this[key] == obj) return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一个关联数组并尝试循环它的键:
var arr = new Array();
arr['One'] = 1;
arr['Two'] = 2;
arr['Three'] = 3;
for(var key in arr)
alert(key);
Run Code Online (Sandbox Code Playgroud)
这会返回五个项目:
-One -Two -Three -containsKey -containsValue
但我想(期待?)只有三个.我接近这个错吗?有没有办法"隐藏"原型方法?或者我应该采取不同的做法?
这看起来很奇怪.
这是我在IE8控制台中的实验:
typeof obj1 // "object"
obj1.hasOwnProperty // {...}
typeof obj2 // "object"
obj2.hasOwnProperty // undefined
Run Code Online (Sandbox Code Playgroud)
关于什么可能导致这个问题的任何想法?
javascript internet-explorer-8 ie-developer-tools hasownproperty
我想检查一个对象是否具有某个属性,并且其值等于某个值.
var test = [{name : "joey", age: 15}, {name: "hell", age: 12}]
Run Code Online (Sandbox Code Playgroud)
你去,一个对象数组,现在我想在对象内搜索,如果对象包含我想要的东西,则返回true.
我试着这样做:
Object.prototype.inObject = function(key, value) {
if (this.hasOwnProperty(key) && this[key] === value) {
return true
};
return false;
};
Run Code Online (Sandbox Code Playgroud)
这可行,但不在数组中.我怎么做?
我正在开发一个UserScript,我认为为Object创建2个Prototype函数会节省更多时间.
Object.prototype.Count = function() {
var size = 0, key;
for (key in this) {
if (this.hasOwnProperty(key)) {
size++;
}
}
return size;
};
Object.prototype.GetEntry = function(index) {
var size = 0, key;
for (key in this) {
if (this.hasOwnProperty(key)) {
if (size == index)
return this[key];
size++;
}
}
return null;
};
Run Code Online (Sandbox Code Playgroud)
这两个函数在我的调试控制台上完全正常工作,因为我输入它们,我使用它们,但是当我运行我的脚本时,它会让我的控制台出现一些奇怪的错误.
Uncaught TypeError: U[a].exec is not a function
Uncaught TypeError: (ec[b] || []).concat is not a function
Uncaught TypeError: X[g].exec is not a function
Uncaught TypeError: (Qn[t] …Run Code Online (Sandbox Code Playgroud) Array.prototype.myFeature = function() {};
var arr = ['some', 'items'];
for (var prop in arr) {
console.log(prop);
}Run Code Online (Sandbox Code Playgroud)
该代码的输出将是:0,1,myFeature.
问题是:为什么只有Array原型的自定义添加功能被输出,而不是原型中存在的所有功能?
我一直在挖掘,但找不到关于如何在 Google App Script 中使用异步函数的参考或文档,我发现人们提到这是可能的,但没有提到如何...
有人可以指出我正确的方向或为我提供一个例子吗?承诺、回调或其他可以帮助我解决这个问题的东西。
我有这个函数可以调用它foo需要一段时间才能执行(足够长的时间它可能会超时 HTTP 调用)。
我想要做的是重构它,它的工作方式是这样的:
function doPost(e) {
// parsing and getting values from e
var returnable = foo(par1, par2, par3);
return ContentService
.createTextOutput(JSON.stringify(returnable))
.setMimeType(ContentService.MimeType.JSON);
}
function foo(par1, par2, par3) {
var returnable = something(par1, par2, par3); // get the value I need to return;
// continue in an Async way, or schedule execution for something else
// and allow the function to continue its flow
/* async bar(); */
return returnable;
} …Run Code Online (Sandbox Code Playgroud) 所以,这里有一些示例javascript代码:
Object.prototype.simpleFunction = function () {
return true;
}
var tempObject = {};
for (var temp in tempObject) {
console.log(temp);
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果执行此操作,您将从console.logGoogle Chrome中的命令获得"simpleFunction"输出.(我用的是19.0.1084.46m.)
但是,各种相关的Object函数都没有传递给console.log.
如何在Object原型中添加函数而不将它们显示在我的" for property in对象"循环中?
编辑:我应该提到我想要的最后一件事是在那里抛出另一个'if'语句,因为它意味着我需要将它添加到所有for循环中.:(
javascript ×9
arrays ×2
for-loop ×2
jquery ×2
loops ×2
promise ×2
async-await ×1
asynchronous ×1
callback ×1
intersection ×1
iteration ×1
node.js ×1
prototype ×1
userscripts ×1
web ×1