本教程演示了如何打印 JavaScript 数组元素。它还取决于您的项目需求,您是要打印整个数组还是特定数量的元素。
或者您想以字符串格式或其他格式打印数组元素。让我们探索打印 JavaScript 数组的不同方法。
目录
1.在JavaScript 中打印数字组元素的不同方式
2.在JavaScript中使用for循环打印数组元素
3.在JavaScript中使用while循环打印数组元素
4.在JavaScript中使用forEach方法打印数组元素
5.在JavaScript中使用console.log()函数打印数组元素
6.在JavaScript中使用map( ) Withjoin()方法打印数组元素
7.在JavaScript 中使用tostring()函数打印数组元素
8.在JavaScript 中使用join()函数打印数组元素
9.在JavaScript 中使用pop()函数打印数组元素
10.在JavaScript中使用shift()方法打印数组元素
11.在JavaScript 中使用concat()方法打印数组元素
12.在JavaScript 中使用slice()方法打印数组元素
1.在JavaScript 中打印数字组元素的不同方式
for
andwhile
循环遍历数组元素,一次一个,每个元素打印在单独的一行上,而我们可以使用该方法获得相同的输出forEach()
。
该forEach()
函数为每个数组元素运行一次特定方法。
我们可以通过将数组名称传递给console.log()
函数来打印所有数组元素。我们也可以使用map()
andjoin()
方法先操作,然后打印数组的元素。
例如,我们想要对输出中的每个数组元素进行编号。
该map()
函数通过为每个元素调用一次函数来创建一个新数组,但不会更改原始数组。请记住,它不会为空元素运行该函数。
该jion函数就像一个分隔符,用指定的分隔符分隔数组的元素。
pop()
返回最后一个元素,同时返回数组的shift()
第一个元素,但是当您希望从字符串中删除这些元素时最好使用这两种方法,因为两者都可以更改原始数组。
该toString()函数将数组转换为字符串格式。 该slice()函数将数组的选定元素作为新数组提供给我们,但不会更改原始数组。
它也可以根据特定start
和end
索引工作(end
索引在这里是唯一的)。
2.在JavaScript中使用for循环打印数组元素
让我们开始使用一个简单的for
循环来打印一个完整的 JavaScript 数组。
var names = ["mehvish", "tahir", "aftab", "martell"];
//for loop
for(var i=0; i<names.length; i++){
console.log(names[i]);
}
输出:
"mehvish"
"tahir"
"aftab"
"martell"
3.在JavaScript中使用while循环打印数组元素
下面让我们看看while
循环是如何产生输出的。
var names = ["mehvish", "tahir", "aftab", "martell"];
var i=0;
//while loop
while(i<names.length){
console.log(names[i]);
i++;
}
输出:
"mehvish"
"tahir"
"aftab"
"martell"
4.在JavaScript中使用forEach方法打印数组元素
你有没有想过forEach
打印一个完整的 JavaScript 数组(每行一个元素)的方法?请参阅下面的代码片段。
var names = ["mehvish", "tahir", "aftab", "martell"];
names.forEach(element => console.log(element));
输出:
"mehvish"
"tahir"
"aftab"
"martell"
5.在JavaScript中使用console.log()函数打印数组元素
我们已经探索了打印完整数组但每行一个元素的方法。您可以按如下方式在一行中打印所有数组元素。
var names = ["mehvish", "tahir", "aftab", "martell"];
console.log(names);
输出:
["mehvish", "tahir", "aftab", "martell"]
6.在JavaScript中使用map( ) Withjoin()方法打印数组元素
我们也可以使用以下方式在一行中显示所有元素。
var names = ["mehvish", "tahir", "aftab", "martell"];
var element = names.map( (e,i) => (i+1+"."+e) ).join(' ');
console.log(element);
输出:
"1.mehvish 2.tahir 3.aftab 4.martell"
7.在JavaScript 中使用tostring()函数打印数组元素
你有没有想过我们有一个名为的函数toString()
可以将数组转换为字符串。请参阅以下代码示例。
var names = ["mehvish", "tahir", "aftab", "martell"];
console.log(names.toString());
输出:
"mehvish,tahir,aftab,martell"
8.在JavaScript 中使用join()函数打印数组元素
上面我们看到数组被转换成了字符串,但是每个元素之间用逗号隔开。如果我们希望将所有元素分隔到一个空间中怎么办?为此,join()
使用该功能。
var names = ["mehvish", "tahir", "aftab", "martell"];
console.log(names.join(" "));
输出:
"mehvish tahir aftab martell"
9.在JavaScript 中使用pop()函数打印数组元素
你想只打印数组的最后一个元素吗?我们可以通过使用pop()
函数来做到这一点。
var names = ["mehvish", "tahir", "aftab", "martell"];
console.log(names.pop());
输出:
"martell"
10.在JavaScript中使用shift()方法打印数组元素
在下面的代码中,该shift()
方法打印数组的第一个元素。
var names = ["mehvish", "tahir", "aftab", "martell"];
console.log(names.shift());
输出:
"mehvish"
11.在JavaScript 中使用concat()方法打印数组元素
我们有两个独立的 JavaScript 数组,并将它们打印在一行中。我们希望第二个数组的元素位于第一个数组的元素之后。
我们将使用该方法将fruitsArrayTwo
with连接起来。fruitsArrayOne
Concat()
var fruitsArrayOne = ["apple", "banana", "grapes"];
var fruitsArrayTwo = ["mango", "water-melon"];
console.log(fruitsArrayOne.concat(fruitsArrayTwo));
输出:
["apple", "banana", "grapes", "mango", "water-melon"]
12.在JavaScript 中使用slice()方法打印数组元素
slice()
我们还可以使用该方法打印特定的元素数组。我们还可以使用该splice()
方法来添加和删除数组的元素。有关详细信息,请查看此页面。
var names = ["mehvish", "tahir", "aftab", "martell"];
console.log(names.slice(0, 2)); //end index is not inclusive here
输出:
["mehvish", "tahir"]