let obj=this.list.find(item=>item.code===val) 怎么在===参数后添加另一个条件达到并且的效果
时间: 2023-08-09 17:09:39 浏览: 45
你可以使用逻辑运算符 `&&` 来连接多个条件,例如:
```
let obj = this.list.find(item => item.code === val && item.status === 'published');
```
这样就会找到 `code` 等于 `val` 并且 `status` 等于 `'published'` 的项。当使用逻辑运算符连接多个条件时,所有条件都必须返回 `true` 才会返回结果,否则返回 `undefined`。
相关问题
this.$u.api.serviceArea.getserviceArea().then(res=>{ this.list = this.list.concat(res); console.log(res) let company1 = []; let company2 = []; for(let i=1;i<this.list.length;i++){ if (this.list[i].treeLevel == 1) { let obj = {name:''}; obj["name"] = this.list[i].companyName company1.push(obj); }else if (this.list[i].treeLevel == 2) { company2.push(this.list[i]) } } this.company = company1; console.log(this.company ) let groups = [...new Set(company2.map(item => item.parentCode))]; let newArr = groups.map(parentCode =>{ let children = company2.filter(item => item.parentCode == parentCode); children.forEach(child => { // 获取当前子项的父项 let parent = company2.find(item => item.parentCode === parentCode); // 将父项的companyName赋值给子项 child.parentName = parent.companyName; }); return children; }); this.child = newArr; console.log(this.child); 如何将treeLevel=1的元素组成一个数组,里面包含treeLevel=2并且parentCode=父节点的companyCode
您可以使用数组的 `filter()` 方法来筛选出 `treeLevel=1` 的元素,然后使用 `map()` 方法对这些元素进行处理。在 `map()` 方法中,您可以使用 `filter()` 方法再次筛选出 `treeLevel=2` 且 `parentCode` 等于当前元素的 `companyCode` 的元素,最后将这些元素添加到当前元素的一个新属性中,例如 `children`。以下是一种可能的实现方式:
```
this.company = [];
this.child = [];
this.$u.api.serviceArea.getserviceArea().then(res => {
this.list = this.list.concat(res);
// Filter out treeLevel=1 elements
const companies1 = this.list.filter(item => item.treeLevel === 1);
// Map through the treeLevel=1 elements
this.company = companies1.map(company1 => {
// Filter out treeLevel=2 elements with matching parentCode
const children = this.list.filter(item => item.treeLevel === 2 && item.parentCode === company1.companyCode);
// Add children to the company1 object as a new property
return {
...company1,
children
};
});
// Filter out all treeLevel=2 elements
const companies2 = this.list.filter(item => item.treeLevel === 2);
// Group treeLevel=2 elements by parentCode
const groups = [...new Set(companies2.map(item => item.parentCode))];
const newArr = groups.map(parentCode => {
const children = companies2.filter(item => item.parentCode === parentCode);
children.forEach(child => {
let parent = companies2.find(item => item.companyCode === parentCode);
child.parentName = parent.companyName;
});
return children;
});
this.child = newArr;
});
```
在上面的代码中,我们首先使用 `filter()` 方法筛选出 `treeLevel=1` 的元素,并将结果存储在 `companies1` 数组中。然后使用 `map()` 方法对 `companies1` 数组中的每个元素进行处理,将符合条件的 `treeLevel=2` 的元素添加到一个新的 `children` 属性中,并将结果存储在 `this.company` 数组中。
最后,我们使用 `filter()` 方法筛选出所有的 `treeLevel=2` 的元素,并使用 `map()` 方法对这些元素进行分组,将每个元素的 `parentCode` 属性作为键,将符合条件的元素分组到一个新的数组中。在分组过程中,我们还使用 `forEach()` 方法将每个元素的 `parentName` 属性设置为其父元素的 `companyName`。最终的结果存储在 `this.child` 数组中。
handerPersonList(list,result,type){ // 获取所有 收藏人员 数据Id let empIds = this.getPersonEmpId(this.latelyList); let arr = []; if(list && list.length > 0){ for(let item of list){ let obj = Object.assign({},item); obj.admin = util.combineName(item); obj.id = ''; obj.aId = ''; obj.bId = ''; obj.picSrc = item.picSrc || require('@/assets/common_images/person.jpg'); // 设置收藏 if(empIds.indexOf(obj.empId) > -1){ let latelyUser = this.latelyList.find(it=>it.empId == obj.empId); if(latelyUser){ obj.id = latelyUser.id; } obj.isCollect = true; } else{ obj.isCollect = false; } arr.push(obj); } } if(type && type == 'allUser'){ this.paginationuser.total = result.pageControllerInfo.totalRecordCount; this.$refs.userTable.bodyWrapper.scrollTop = 0; this.userdata = arr; }else{ this.orgPersonList = arr; this.pagination.total = result.pageControllerInfo.totalRecordCount; this.$refs.employeeTable.bodyWrapper.scrollTop = 0; } },
这段代码主要是用来处理收藏人员列表数据的。它接收三个参数,分别是 `list`,`result` 和 `type`。
首先,代码会使用 `getPersonEmpId` 函数获取所有收藏人员的 ID,然后对 `list` 数组进行遍历,对每个元素进行处理。处理后的每个元素会被添加一些属性,比如 `admin`,`id`,`aId`,`bId`,`picSrc` 和 `isCollect` 等。其中,`admin` 表示人员名称,`id` 表示收藏人员 ID,`aId` 和 `bId` 分别表示一级部门和二级部门的 ID,`picSrc` 表示人员头像,`isCollect` 表示人员是否被收藏。
如果 `empIds.indexOf(obj.empId) > -1`,则表示当前人员已被收藏。此时,代码会在 `latelyList` 中查找该人员并获取其 ID,然后将该 ID 赋值给 `obj.id`。最后,设置 `obj.isCollect` 为 `true`。
如果 `empIds.indexOf(obj.empId) < 0`,则表示当前人员未被收藏。此时,代码会将 `obj.isCollect` 设置为 `false`。
最后,如果 `type` 的值为 `allUser`,则将处理后的数据赋值给 `this.userdata`,否则赋值给 `this.orgPersonList`。