我有一个包含元素的数组,例如 array = ["example1", "example2", "example3"]。
如何按以下格式打印?
使用
forEach
如下所示
var a = ["a", "b", "c"];
a.forEach(function(entry) {
console.log(entry);
});
您可以使用简单的
for
循环:
for (i = 0; i < array.length; i++)
document.writeln((i+1) + ": " + array[i]);
并使用
document.writeln
将其打印出来。请参阅下面的工作片段。
片段
array = ["example1", "example2", "example3"];
for (i = 0; i < array.length; i++)
document.writeln((i+1) + ": " + array[i]);
注:
多次以不同方式实现。所以你应该使用:document.writeln()
document.getElementById("id_of_div").innerHTML += (i+1) + ": " + array[i];
使用 forEach 如下所示(ES6)
var a = ["a", "b", "c"];
a.forEach((element) => {
console.log(element)
}
);
您可以使用标准数组方法来获得您想要的结果。 MDN 有一些关于数组迭代方法的很棒的文档。
var examples = ["example1", "example2", "example3"];
// You can use reduce to transform the array into result,
// appending the result of each element to the accumulated result.
var text = examples.reduce(function (result, item, index) {
var item_number = index + 1;
return result + " " + item_number + ". " + item;
}, "");
// You can use a variable with forEach to build up the
// result - similar to a for loop
var text = "";
examples.forEach(function (item, index) {
var item_number = index + 1;
text = text + " " + item_number + ". " + item;
});
// You can map each element to a new element which
// contains the text you'd like, then join them
var text = examples.map(function (item, index) {
var item_number = index + 1;
return item_number + ". " + item;
}).join(" ");
// You can put them into an HTML element using document.getElementById
document.getElementById("example-text-result").innerHTML = text;
// or print them to the console (for node, or in your browser)
// with console.log
console.log(text);
尝试使用 for 循环:
for (var i=0; i<array.length; i++)
console.log(i + ". " + array[i]);
你可以试试这个:-
var arr = ['example1', 'example2', 'example3']
for (var i = 0; i < arr.length; i++){
console.log((i + 1) + '. ' + arr[i])
}
您可以执行以下操作将其打印成一行:
let exampleArray = ["example1", "example2", "example3"];
let result = [];
for (let i = 0; i < exampleArray.length; i++) {
result += i + 1 + ". " + exampleArray[i] + " ";
}
console.log(result);
我正在解决这样的问题,我做了这个
const multiLanguages = ["C is fun", "Python is cool", "JavaScript is amazing"];
for (let i = 0; i < multiLanguages.length; i++) {
console.log(multiLanguages[i]);
}
如果你想在 ES6 中做到这一点,reduce 将是你比 map 更好的选择。它为你声明了预定义的变量和事件你不需要 join()
var array = ["example1", "example2", "example3"];
var op= array.reduce((ac,cur,ind)=> ac +` ${ind+1}.` + cur,'');
console.log(op);
使用 Map() 和 Join()
var array = ["example1", "example2", "example3"];
var op= array.map((e, i) =>`${i+1}.${e}`).join(' ');
console.log(op);
只是我的两分钱:
let array = [[['a','b',['c', 13, ['d', 22, [44, [34]]]]],[1,2,3]],[],[['d','e','f','g','h'],[4,5,6,7]]];
console.log(printArray(array));
function printArray(inputArr) {
return "[" + _printArray(inputArr).intermediateVal + "]";
}
function _printArray(inputArr) {
if(Array.isArray(inputArr)) {
let str = "";
if(Array.isArray(inputArr)){
for(let a=0; a<inputArr.length; a++) {
let result = _printArray(inputArr[a]);
if(result.isComplexType) {
str += "[" + result.intermediateVal + "]";
} else {
str +=result.intermediateVal;
}
if(a+1 < inputArr.length) {
str += ", ";
}
}
}
return {
"isComplexType": true,
"intermediateVal": str
}
} else {
return {
"isComplexType": false,
"intermediateVal": inputArr
}
}
}
并品脱这个:
[[[a, b, [c, 13, [d, 22, [44, [34]]]]], [1, 2, 3]], [], [[d, e, f, g, h], [4, 5, 6, 7]]]