JavaScript :在同一循环中迭代推送的元素
JavaScript : Iterate pushed elements in a same loop
如何在同一循环中迭代推送值。
我有一个array
喜欢,
var arrays = [
"hii",
"hello"
];
我使用 for in
和 push
、
进行迭代
for (var index in arrays) {
arrays.push(arrays[index] + " world");
console.log(arrays[index]);
}
console.log(arrays);
输出:
hii
hello
Array(4): 0 : "hii", 1 : "hello", 2 : "hii world", 3 : "hello world"
问题:如何迭代推送的元素并在同一循环中迭代?
映射并展平
const arrays = [
"hii",
"hello"
];
let x=arrays.map(a=>[a,a+' world']).flat();
console.log(x);
这是你想要的吗?
for (var index in arrays) {
arrays[index] += " world";
console.log(arrays[index]);
}
编辑:OP 编辑了问题以在迭代中也包含推送的元素
如果你想在迭代过程中包含推送的元素(你声称有条件地这样做以防止无穷大),你可以使用 for
循环和 Array.length
。
for (var i = 0; i < arrays.length; i++) {
if (some_condition)
arrays.push(arrays[i] + 'world');
}
循环将始终尝试达到新的长度。
上一个答案:
Array.forEach
方法不会将迭代器应用于在迭代期间被推送的元素。
const arrays = [
"hii",
"hello"
];
arrays.forEach((e, i, a) => {
a.push(e + ' world')
});
console.log(arrays)
您可以按如下方式进行
[...arrays,...arrays.map(a=>a+' world')]
例子如下
var arrays = [
"hii",
"hello"
];
function appendWorld(arr){
return [...arrays,...arrays.map(a=>a+' world')]
}
console.log(appendWorld(arrays))
这可能是最简单的答案:
var b=["hi",'hello'];
var arrayLength = b.length;
for(var i=0;i<arrayLength*2;i++){
if(i < arrayLength) b.push(b[i] + " world");
console.log(b[i]);
}
console.log(b);
输出:
将长度设置为硬编码 *2,因为您只想在循环中添加一次元素。
我猜这就是你想要的。
var arrays = [
"hii",
"hello"
];
for(var i = 0; i < arrays.length; i++){
arrays.push(arrays[i] + " world");
console.log(arrays[i]);
/* This line breaks the code*/
if(i == 10) break
}
console.log(arrays)
输出:
hii
hello
hii world
hello world
hii world world
hello world world
hii world world world
hello world world world
hii world world world world
hello world world world world
hii world world world world world
["hii", "hello", "hii world", "hello world", "hii world world", "hello world world", "hii world world world", "hello world world world", "hii world world world world", "hello world world world world", "hii world world world world world", "hello world world world world world", "hii world world world world world world"]
这肯定会遍历新元素。我不确定你是否想这样做,因为它会在没有 break 语句的情况下进行无限循环。
你怎么不用一会儿?
let myArray = ["one", "two", "three"];
let i = 0;
while (myArray.lenght > i)
{
if (anyCondition)
{
myArray.push('aló')
}
i++;
}
如何在同一循环中迭代推送值。
我有一个array
喜欢,
var arrays = [
"hii",
"hello"
];
我使用 for in
和 push
、
for (var index in arrays) {
arrays.push(arrays[index] + " world");
console.log(arrays[index]);
}
console.log(arrays);
输出:
hii
hello
Array(4): 0 : "hii", 1 : "hello", 2 : "hii world", 3 : "hello world"
问题:如何迭代推送的元素并在同一循环中迭代?
映射并展平
const arrays = [
"hii",
"hello"
];
let x=arrays.map(a=>[a,a+' world']).flat();
console.log(x);
这是你想要的吗?
for (var index in arrays) {
arrays[index] += " world";
console.log(arrays[index]);
}
编辑:OP 编辑了问题以在迭代中也包含推送的元素
如果你想在迭代过程中包含推送的元素(你声称有条件地这样做以防止无穷大),你可以使用 for
循环和 Array.length
。
for (var i = 0; i < arrays.length; i++) {
if (some_condition)
arrays.push(arrays[i] + 'world');
}
循环将始终尝试达到新的长度。
上一个答案:
Array.forEach
方法不会将迭代器应用于在迭代期间被推送的元素。
const arrays = [
"hii",
"hello"
];
arrays.forEach((e, i, a) => {
a.push(e + ' world')
});
console.log(arrays)
您可以按如下方式进行
[...arrays,...arrays.map(a=>a+' world')]
例子如下
var arrays = [
"hii",
"hello"
];
function appendWorld(arr){
return [...arrays,...arrays.map(a=>a+' world')]
}
console.log(appendWorld(arrays))
这可能是最简单的答案:
var b=["hi",'hello'];
var arrayLength = b.length;
for(var i=0;i<arrayLength*2;i++){
if(i < arrayLength) b.push(b[i] + " world");
console.log(b[i]);
}
console.log(b);
输出:
将长度设置为硬编码 *2,因为您只想在循环中添加一次元素。
我猜这就是你想要的。
var arrays = [
"hii",
"hello"
];
for(var i = 0; i < arrays.length; i++){
arrays.push(arrays[i] + " world");
console.log(arrays[i]);
/* This line breaks the code*/
if(i == 10) break
}
console.log(arrays)
输出:
hii
hello
hii world
hello world
hii world world
hello world world
hii world world world
hello world world world
hii world world world world
hello world world world world
hii world world world world world
["hii", "hello", "hii world", "hello world", "hii world world", "hello world world", "hii world world world", "hello world world world", "hii world world world world", "hello world world world world", "hii world world world world world", "hello world world world world world", "hii world world world world world world"]
这肯定会遍历新元素。我不确定你是否想这样做,因为它会在没有 break 语句的情况下进行无限循环。
你怎么不用一会儿?
let myArray = ["one", "two", "three"];
let i = 0;
while (myArray.lenght > i)
{
if (anyCondition)
{
myArray.push('aló')
}
i++;
}