[1,2,3].forEach(function(el) {
if(el === 1) break;
});
Run Code Online (Sandbox Code Playgroud)
如何forEach在JavaScript中使用新方法执行此操作?我试过了return;,return false;而且break.break崩溃,return除了继续迭代之外什么都不做.
假设我有一个这样的循环:
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Run Code Online (Sandbox Code Playgroud)
快速问题:是否return停止循环执行本身?
我在尝试解决的代码中有错误。我认为它需要一个return语句,但我已经在forEach循环之外,但它仍然抛出错误:
not all the code path return the value
Run Code Online (Sandbox Code Playgroud)
如何修复下面的代码?
main.ts:
private ValidateRequestArgs(str) {
let ret: boolean = true;
// here on val its throwing tslint error not all code paths return value
str.split(',').forEach((val) => {
if (!ret) {
return false;
}
if (List.indexOf(val) > -1) {
ret = true;
} else {
ret = false;
}
});
return ret;
}
Run Code Online (Sandbox Code Playgroud) Object.prototype.e = function() {
[].forEach.call(this, function(e) {
return e;
});
};
var w = [1,2];
w.e(); // undefined
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用警报,这是有效的
// ...
[].forEach.call(this, function(e) {
alert(e);
});
// ...
w.e(); // 1, 2
Run Code Online (Sandbox Code Playgroud) 我只是创建一个函数来检查我的对象数组中的某个值,但由于某种原因它会一直返回undefined.这是为什么?
演示:http://jsfiddle.net/cNYwz/1/
var data = [{
"Key": "1111-1111-1111",
"Email": "test@test.com"
}, {
"Key": "2222-2222-2222",
"Email": "test@boo.com"
}];
function getByKey(key) {
data.forEach(function (i, val) {
if (data[val].Key === key) {
return data[val].Key;
} else {
return "Couldn't find";
}
});
}
var asd = getByKey('1111-1111-1111');
console.log(asd);
Run Code Online (Sandbox Code Playgroud) 我试图确定数组是否包含某个项目.如果是,我想保留该功能,否则应该添加.
function addPacking(item){
data.packings.forEach(function(entry){
if(item.name == entry.name){
return;
}
});
data.packings.push(item);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,即使满足if条件,也会推送数据.如何在不使用else条件的情况下阻止此行为?
(我不想使用,else因为我的实际代码比这复杂得多,我想让它保持可读性)
编辑:
是forEach异步执行吗?
我想知道JavaScript中的forEach是否可以返回,这是我的代码:
var a = [0, 1, 2, 3, 4];
function fn(array) {
array.forEach(function(item) {
if (item === 2) return false;
});
return true;
}
var ans = fn(a);
console.log(ans); // true
Run Code Online (Sandbox Code Playgroud)
我想知道2是否在我的数组中,如果是,返回false,但似乎forEach函数已经循环整个数组并忽略return.
我想知道我是否能得到我想要的答案forEach(我知道我可以得到我想用的(...))?亲爱的朋友,请帮助我,非常感谢!
我创建了一个数组和 var:
var database = [
{
username:"candy",
password:"666"
},
{
username: "abby",
password: "123"
},
{
username: "bob",
password: "777"
}
];
var newsFeed = [
{
username: "Bob",
timeline: "So tired from all that learning!"
},
{
username: "Midori",
timeline: "Javascript is sooooo cool!"
},
{
username: "Abby",
timeline: "Javascript is preeetyy cool!"
}
]
var usernamePrompt = prompt("What\'s your username?");
var passwordPrompt = prompt("What\'s your password?");
Run Code Online (Sandbox Code Playgroud)
我想使用 forEach 遍历所有数组值var database,然后检查它们是否true是false
其余代码:
database.forEach(checkDatabase2);//using …Run Code Online (Sandbox Code Playgroud) 所以我试图在 Angular 中从数组中执行 POST 请求。基本上,当用户选择列表中的多个项目时,他们可以“解锁”每个项目。所以我遇到的问题是如何使用 forEach 进行 POST。我能够使用 forLoop 执行 POST,但问题是当它执行一个 POST 时,它不会执行另一个。有人可以指出我做错了什么,或者是否有更好的解决方案来解决这个问题?
以下是我查看的其他堆栈问题以找到可能的解决方案:
组件.ts
locked: Array<any> = [];
// Unlock
unlock() {
let observer = {
next(data) {
data => console.log(data)
},
error(error) {
return new Error('Post Error');
},
complete() {
console.log("Completed");
// window.location.reload();
}
}
// On unlock send lock data to Service for POST
this.http.postUnlock(this.unlocked).subscribe(observer);
}
Run Code Online (Sandbox Code Playgroud)
服务.ts
// POST to UNLOCK RESOURCES
postUnlock(locked) {
let headers = new …Run Code Online (Sandbox Code Playgroud) 我有一个位置数组,试图在页面上显示其值,我使用了以下代码来遍历该数组:
{this.props.locations && this.props.locations.forEach(loc => {
console.log("Location: "+loc)
return(
<span>Location is: {loc}</span>
)
})} Run Code Online (Sandbox Code Playgroud)
页面上的结果为空: 在此处输入图像描述, 但是它正确记录了位置: 在此处输入图像描述
我在App.js中获得this.props.locations的值,如下所示:
var locations = this.state.cardLists
var distictLocations = new Set()
locations.forEach(location => {
if(!distictLocations.has(location.Location))
{
distictLocations.add(location.Location)
}
});
this.setState({
locations: distictLocations
})Run Code Online (Sandbox Code Playgroud)
我不确定自己在做什么错。任何帮助,将不胜感激。
这是我的 JavaScript 函数,用于检查数组中是否存在特定条目,但即使存在匹配项,它也始终返回 null
function getSupplierForId(supplierId) {
data.suppliers.forEach(x => {
console.log('Current item id : ' + x.id);
console.log('Requested supplier id : ' + supplierId);
console.log('Both ids are equal : ' + (x.id === supplierId));
if (x.id === supplierId) {
console.log(x);
return x;
}
});
return null;
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制台输出:
为什么它总是返回 null?