js遍历数组对象拼接换行
时间: 2023-10-01 12:03:39 浏览: 133
javascript制作sql转换为stringBuffer的小工具
可以使用for循环或者forEach遍历数组对象,然后通过字符串拼接的方式加入换行符"\n"即可。例如:
```javascript
var fruits = [
{name: "apple", color: "red"},
{name: "banana", color: "yellow"},
{name: "grape", color: "purple"}
];
var output = "";
fruits.forEach(function(fruit) {
output += fruit.name + " - " + fruit.color + "\n";
});
console.log(output);
```
输出结果如下:
```
apple - red
banana - yellow
grape - purple
```
阅读全文