vue foreach循环遍历数组_vue中遍历数组中的一个对象中的值
vue foreach循环遍历数组_vue中遍历数组中的一个对象中的值数组字转符串拼接成逗号去掉最后的逗号vararr=[2,3,4,5]varstr="";for(vari=0;i<arr.length;i++){str+=arr[i]+",";}//去掉最后一个逗号(如果不需要去掉,就不用写)if(str.length>0){str=str.substr(0,str.length-1);}或者直接判...
数组字转符串拼接成逗号去掉最后的逗号
var arr=[2,3,4,5]
var str = "";
for (var i = 0; i < arr.length; i++) {
str += arr[i]+ ",";
}
//去掉最后一个逗号(如果不需要去掉,就不用写)
if (str.length > 0) {
str = str.substr(0, str.length - 1);
}
或者直接判断
var arr = [2, 7, 9, 7, 9, 7];
var str1 = "";
for (let i = 0; i < arr.length; i++) {
if (i < arr.length - 1) {
str1 += arr[i] + ",";
} else {
str1 += arr[i];
}
}
//输出字符串的2,7,9,7,9,7
拼接对象
for (let i = 0; i < idarr.length; i++) {
let o = {};
o.id = idarr[i];
o.label = namearr[i];
that.toData.push(o);
}
或者
this.TableData.forEach(item => {
const newInfo = {
id: item.attr id,
lable: item.attr_vals.join('')
attrs.push(newInfo)
})
只听到从架构师办公室传来架构君的声音:
伐柯如何?匪斧不克。有谁来对上联或下联?
伐柯如何?匪斧不克。有谁来对上联或下联?
js forEach for区别
1、循环中断差别
具体见示例代码:
此代码由Java架构师必看网-架构君整理<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>js for forEach区别 </title> </head> <body> <script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script> <script type="text/javascript"> let arr = [1, 2, 3, 4] for(let i = 0; i < arr.length; i++) { if(arr[i] == 2) { continue; //break; } console.log(arr[i], ' for') } for(let i in arr) { if(i == 2) { break; } console.log(arr[i], ' for in') } for(let i of arr) { if(i == 2) { break; } console.log(i, ' for of') } arr.forEach(val => { if(val == 2) { //此处的return false 仅仅相当于continue return false; //break或者continue均不能使用 会报错,对于map filter方法一样会报错 //break; //continue } console.log(val, ' forEach') }) </script> </body> </html>
数组的迭代方法:every、filter、forEach、map、some均不能使用break或者continue进行中断循环。
以上几个函数的参数都是:一个回调函数 和 一个this的指向
array.map(function(currentValue,index,arr), thisValue)
2、数组变化时差别
(1)数组添加操作
此代码由Java架构师必看网-架构君整理<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>js for forEach区别 </title> </head> <body> <script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script> <script type="text/javascript"> let arr = [1, 2, 3, 4] for(let i = 0; i < arr.length; i++) { if(arr[i] == 2) { //对于添加时,for可以遍历新数组 arr.push(5) } // 输出1 2 3 4 5 console.log(arr[i], ' for') } arr.forEach(val => { if(val == 2) { //对于添加时,forEach arr.push(5) } // 输出1 2 3 4 5 console.log(val, ' forEach') }) </script> </body> </html>
(2)数组更新、删除操作
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>js for forEach区别 </title>
</head>
<body>
<script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script>
<script type="text/javascript">
let arr = [1, 2, 3, 4]
for(let i = 0; i < arr.length; i++) {
if(arr[i] == 2) {
//对于更新、删除时,for可以遍历新数组
arr[1] = 100
//arr.splice(i,1)
}
// 输出1 100 3 4
console.log(arr[i], ' for')
}
arr.forEach((val, i) => {
if(val == 2) {
//对于更新、删除时,forEach可以遍历新数组
val = 100
//arr.splice(i,1)
}
// 输出1 100 3 4
console.log(val, ' forEach')
})
</script>
</body>
</html>