当前位置:网站首页>every()、map()、forEarch()方法。数组里面有对象的情况

every()、map()、forEarch()方法。数组里面有对象的情况

2022-06-24 07:06:00 隐密修罗

every()方法
这个是数组和对象:
方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。
every() 方法使用指定函数检测数组中的所有元素:

如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
如果所有元素都满足条件,则返回 true。
注意: every() 不会对空数组进行检测。
注意: every() 不会改变原始数组。

<template>
<button  @click="chooseButton(index)" :class="item.myShow===true?'collectTwo' : 'my_choose'" ></button>
</template>
export default{
    
		data(){
    
		return{
    
			collestList:[
				{
    
					myshow:false
				},
				{
    
					myshow:false
				},
				{
    
					myshow:false
				}
			]
		}
	}
method:{
    
		// 选中的项
		chooseButton:function(res){
    
			// 判断是否全部选中了
		    let  myselse = 	this.collestList.every(item=>{
    if (item.myShow==true) return true })
			console.log(myselse)//输出为false
		},
	}
}

在这里插入图片描述
map()方法
作用:通过指定函数对数组进行处理,并将处理后的结果以新数组的形式返回

注意点:不会改变原数组,只是将处理后的结果以新数组的形式返回

var users = [
  {
    name: "张含韵", "email": "[email protected]"},
  {
    name: "江一燕",   "email": "[email protected]"},
  {
    name: "李小璐",  "email": "[email protected]"}
];

var emails = users.map(function (user) {
     return user.email; });
console.log(emails);
//["[email protected]", "[email protected]", "[email protected]"]

forEach() 方法
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
注意: forEach() 对于空数组是不会执行回调函数的。

this.collestList.forEach((value , index) => {
    
								///这是在collestList里面添加myShow:false 的字段
								value['myShow1'] = false 
								
							})

///变成这样的
			collestList:[
				{
    
					myshow:false,
					myshow1:false
				},
				{
    
					myshow:false,
					myshow1:false
				},
				{
    
					myshow:false,
					myshow1:false
				}
			]
原网站

版权声明
本文为[隐密修罗]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_18390335/article/details/111230339