我是JavaScript的新手,我现在正在学习所谓的for... in循环.
在JavaScript中编码时是否真的使用了这些循环?
我可以看到所有其他类型的循环是如何有用的 - 但不是这个.
如果可能的话,有人会对此有所了解并包括一个真实的例子.
我是一个 JS 新手,正在努力理解如何在使用 for in 循环时从对象中获取值。请大家与我分享您的知识。谢谢!
这是我的问题,下面的代码仅记录属性,我尝试使用 for-in 循环来查找对象是否包含值“apple”
var mac = {
company: 'apple',
product: 'iPhone',
price: 300
};
for (var key in mac) {
console.log(key);
}
Run Code Online (Sandbox Code Playgroud) 在 PL/SQL 中,我可以使用 FOR IN 循环从 1 到 20 进行迭代,方法是:
FOR counter IN 1..20
Run Code Online (Sandbox Code Playgroud)
我想知道的是,是否可以对此进行简单的更改,以便仅对 1-20 之间的偶数值迭代循环?即计数器将变为 2, 4, 6, ... 20
在 javascript 中,我试图从对象数组中列出的几个对象中打印值。问题与使用 for in 循环钻取对象/数组有关。更具体地说,如果可能的话,我想避免使用 for 循环(我刚刚学习了 for/in 并试图很好地理解它)
该对象如下所示:
var users = {
'Students': [
{ first_name: 'Michael', last_name : 'Jordan' },
{ first_name : 'John', last_name : 'Rosales' },
{ first_name : 'Mark', last_name : 'Guillen' },
{ first_name : 'KB', last_name : 'Tonel' }
],
'Instructors': [
{ first_name : 'Michael', last_name : 'Jackson' },
{ first_name : 'Martin', last_name : 'Puryear' }
]
};
Run Code Online (Sandbox Code Playgroud)
我怎么能写一个函数来返回这个对象中埋藏的所有名字?我想控制台日志:
Students
1 - MICHAEL JORDAN
2 - JOHN ROSALES …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试学习如何通过treehouse.com编写swift代码,到目前为止我很享受.我刚刚来到我的第一个"代码挑战",因为他们称之为我有点挣扎.正如标题所示,以一个空数组开头:
var results: [Int] = []
Run Code Online (Sandbox Code Playgroud)
这很好,花花公子.目标是然后写一个for循环,找到数字1-10的倍数为6,然后将这些值附加到数组.我最终弄清楚了,但我不确定我是否以理想的方式做到了.我很感激任何批评.我的代码将包含在下面.请记住,我是Swift的初学者和一般的编码.提前感谢任何建议/提示.
var results: [Int] = []
for multiplier in 1...10 {
let multiples = (multiplier * 6)
print(multiples)
results.append(multiples)
}
Run Code Online (Sandbox Code Playgroud)
代码正确执行并且数组附加了值,但同样,我不确定是否有更好的方法来执行此操作.
我有一个名为 objToArray 的空数组。如果键的值大于或等于 2,我正在尝试使用 for-in-loop 用哈希 checkObj 对象中的所有数字填充数组。
const checkObj = {
oddNum: 1,
evenNum: 2,
foundNum: 5,
randomNum: 18
};
const objToArray = [];
for (let values in checkObj) {
if (Object.values(checkObj) >=2 ) {
objToArray.push(checkObj.values())
}
}
console.log(objToArray);Run Code Online (Sandbox Code Playgroud)
到目前为止,我得到 objToArray 作为一个空数组,即使它应该包含三个元素并且应该等于 [2, 5, 18]。
var result = function(){}
console.log("constructor" in result);//true
console.log("__proto__" in result);//true
console.log("prototype" in result);//true
for (var prop in result) {
console.log(prop); // no output!!
}
Run Code Online (Sandbox Code Playgroud)
当我in用于确定结果中的属性时,它返回true; 但是当我使用时for-in,结果中没有属性,为什么?
我想在Arrays上添加一个"插入"方法.所以我这样做:
> Array.prototype.insert = function(index, element){
this.splice(index, 0, element);
};
Run Code Online (Sandbox Code Playgroud)
它有效:
> a = [1,2,3]
[1, 2, 3]
> a.insert(0, 4)
undefined
> a
[4, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
但是有一种不良的副作用:
> for (i in a){console.log(i)}
0
1
2
3
insert
> for (i in a){console.log(a[i])}
4
1
2
3
function (index, element){
this.splice(index, 0, element);
}
Run Code Online (Sandbox Code Playgroud)
此行为不是打算,并打破我使用的其他库.这有什么优雅的解决方案吗?
这是代码:
var InvertedPeninsula = function() {
this.inhabitants = [
{
name: 'Sir Charles',
race: 'Human'
},
{
name: 'Ealei',
race: 'Elf'
}
];
// Adds an extra humans method property to the inhabitants array to return all Humans
this.inhabitants.humans = function() { /* returns all Human inhabitants */ };
};
// Create a new invertedPeninsula
var invertedPeninsula = new InvertedPeninsula();
// Log the name of each invertedPeninsula inhabitant
for (var i in invertedPeninsula.inhabitants) {
console.log(invertedPeninsula.inhabitants[i].name);
}
Run Code Online (Sandbox Code Playgroud)
对我来说,它看起来像是2x.3x来自哪里?阵列中只有2个单元格.
我可以通过这段代码在Swift中使用for循环
for i in 0..<5
{
print ("Four multiplied by \(i) results in \(i*4)" )
}
Run Code Online (Sandbox Code Playgroud)
但是我如何使用低于">"条件的for.
for i in 10>..5
{
print ("Four multiplied by \(i) results in \(i*4)" )
}
Run Code Online (Sandbox Code Playgroud)
它显示 错误:'>'不是10>中的i的后缀一元运算符