如何枚举JavaScript对象的属性?
我实际上想要列出所有已定义的变量及其值,但我已经了解到定义变量实际上会创建窗口对象的属性.
Lint错误消息:
src/app/detail/edit/edit.component.ts [111,5]:for(... in ...)语句必须用if语句过滤
代码片段(这是一个工作代码.它也可以在angular.io表单验证部分获得):
for (const field in this.formErrors) {
// clear previous error message (if any)
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessages[field];
for (const key in control.errors) {
this.formErrors[field] += messages[key] + ' ';
}
}
}
Run Code Online (Sandbox Code Playgroud)
知道如何修复这个lint错误吗?
mapunderscore.js中的函数,如果使用javascript对象调用,则返回从对象的值映射的值数组.
_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
=> [3, 6, 9]
Run Code Online (Sandbox Code Playgroud)
有没有办法让它保存钥匙?即,我想要一个返回的函数
{one: 3, two: 6, three: 9}
Run Code Online (Sandbox Code Playgroud) 我有这个对象.我想在JavaScript中迭代这个对象.这怎么可能?
var dictionary = {
"data": [
{"id":"0","name":"ABC"},
{"id":"1","name":"DEF"}
],
"images": [
{"id":"0","name":"PQR"},
{"id":"1","name":"xyz"}
]
};
Run Code Online (Sandbox Code Playgroud) 我知道有很多这样的话题.我知道基础知识:.forEach()在原始数组和.map()新数组上运行.
就我而言:
function practice (i){
return i+1;
};
var a = [ -1, 0, 1, 2, 3, 4, 5 ];
var b = [ 0 ];
var c = [ 0 ];
console.log(a);
b = a.forEach(practice);
console.log("=====");
console.log(a);
console.log(b);
c = a.map(practice);
console.log("=====");
console.log(a);
console.log(c);
Run Code Online (Sandbox Code Playgroud)
这是输出:
[ -1, 0, 1, 2, 3, 4, 5 ]
=====
[ -1, 0, 1, 2, 3, 4, 5 ]
undefined
=====
[ -1, 0, 1, 2, 3, 4, 5 ] …Run Code Online (Sandbox Code Playgroud) 我正在尝试遍历嵌套对象以检索由字符串标识的特定对象.在下面的示例对象中,标识符字符串是"label"属性.我无法绕过如何遍历树以返回适当的对象.任何帮助或建议将不胜感激.
var cars = {
label: 'Autos',
subs: [
{
label: 'SUVs',
subs: []
},
{
label: 'Trucks',
subs: [
{
label: '2 Wheel Drive',
subs: []
},
{
label: '4 Wheel Drive',
subs: [
{
label: 'Ford',
subs: []
},
{
label: 'Chevrolet',
subs: []
}
]
}
]
},
{
label: 'Sedan',
subs: []
}
]
}
Run Code Online (Sandbox Code Playgroud) 我有一个像变量
var files = {
'foo.css': 'foo.min.css',
'bar.css': 'bar.min.css',
};
Run Code Online (Sandbox Code Playgroud)
我希望gulp为我做的是minify文件,然后rename是我.
但是这些任务目前写成(对于一个文件)
gulp.task('minify', function () {
gulp.src('foo.css')
.pipe(minify({keepBreaks: true}))
.pipe(concat('foo.min.css'))
.pipe(gulp.dest('./'))
});
Run Code Online (Sandbox Code Playgroud)
如何重写所以它适用于我files上面定义的变量?
迭代数组的myarray=[1, 2, 3]工作原理如下:
<template is="dom-repeat" items="[[myarray]]">
<span>[[item]]</span>
</template>
Run Code Online (Sandbox Code Playgroud)
如何迭代对象myobject = {a:1, b:2, c:3}?
我有一个关联数组,里面有两个对象.通过此操作$(myassoc).each(),回调只运行一次.回调参数(索引和对象)也分别返回0和整个关联数组.
人们期望jQuery.each()为数组中的每个元素运行,将正确的键作为索引返回,将正确的元素作为对象返回.
为什么不发生这种情况,jQuery可以做我想要的事情吗?
我发现JavaScript函数getElementsByTagName根据浏览器返回不同的数据.与Firefox,IE或Chromium相比,Chrome发送回来的HTML集合更长(真的,更好,IMO).
我将在下面概述我的发现.我的问题基本上是"为什么Chrome会改变这种情况,其他浏览器也会这样做(何时?),以及返回length属性的可靠性如何?"
比较Chrome(版本34.0.1847.116 m)与Chromium(版本33.0.1750.152 Ubuntu 13.10(256984)).我注意到这个Chromium版本有点落后于Chrome(v33 vs v34),因此在Ubuntu Chromum版本中也可能采用这种方式,但它至少帮助我比较了这里发生的事情.
请考虑以下代码块:
<script type='text/javascript'>
function getElements(){
var x=document.getElementsByTagName("input");
console.log(x.length);
console.log(x);
}
</script>
<form>
<input type="text" size="20" id='test1'><br>
<input type="text" size="20" id='test2'><br>
<input type="text" size="20" id='test3'><br><br>
<input type="button" onclick="getElements()" value="How many input elements?">
</form>
Run Code Online (Sandbox Code Playgroud)
在Chromium和其他浏览器中运行上面的结果显示结果显示长度为4,并且返回的数据是索引数组,如:
[input#test1, input#test2, input#test3, input, item: function]
0: input#test1
1: input#test2
2: input#test3
3: input
length: 4
__proto__: NodeList
Chrome会返回类似但扩展的结果数组:
[input#test1, input#test2, input#test3, input, test1: input#test1, test2: input#test2, test3: input#test3, item: function, namedItem: function]
0: input#test1
1: …
javascript ×7
arrays ×2
angular ×1
angular-cli ×1
chromium ×1
css ×1
foreach ×1
gulp ×1
gulp-watch ×1
iteration ×1
jquery ×1
lodash ×1
loops ×1
polymer ×1
polymer-1.0 ×1
properties ×1
templates ×1
tslint ×1