selection.map
时间: 2023-12-25 22:29:08 浏览: 42
`selection.map`是一个数组方法,用于对数组中的每个元素进行操作并返回一个新的数组。它接受一个回调函数作为参数,该回调函数将应用于数组中的每个元素,并将每个元素的处理结果存储在新的数组中。
以下是一个示例,演示了如何使用`selection.map`方法:
```javascript
const selection = [
{ id: 1, nickName: 'John' },
{ id: 2, nickName: 'Jane' },
{ id: 3, nickName: 'Bob' }
];
const ids = selection.map(item => item.id);
console.log(ids); // 输出:[1, 2, 3]
const names = selection.map(item => item.nickName);
console.log(names); // 输出:['John', 'Jane', 'Bob']
```
在上面的示例中,`selection.map`方法将遍历`selection`数组中的每个元素,并将每个元素的`id`属性存储在`ids`数组中,将每个元素的`nickName`属性存储在`names`数组中。
相关问题
selection.map vue.js
在Vue.js中,selection.map()是用于获取el-table中选中的数据的方法。它可以将选中的数据映射为一个新的数组,该数组包含选中数据的指定属性。例如,在上述引用中,handleSelectionChange()方法使用selection.map()方法获取了el-table中选中的数据的id和seatSn属性,并将它们分别存储在ids和seatSn变量中。
下面是一个简单的示例,演示如何使用selection.map()方法获取el-table中选中的数据的id属性:
```javascript
<template>
<div>
<el-table :data="tableData" @selection-change="handleSelectionChange">
<el-table-column type="selection"></el-table-column>
<el-table-column prop="id" label="ID"></el-table-column>
<el-table-column prop="name" label="Name"></el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Bob' }
],
selectedIds: []
}
},
methods: {
handleSelectionChange(selection) {
this.selectedIds = selection.map(item => item.id)
}
}
}
</script>
```
在上述示例中,我们使用了el-table组件来渲染一个表格,并绑定了@selection-change事件来监听选中数据的变化。在handleSelectionChange()方法中,我们使用selection.map()方法将选中的数据的id属性映射为一个新的数组,并将其存储在selectedIds变量中。
以下代码删除item里的id page.multipleSelection.map((item) => { item.goodsId = item.id; item.purchaseId = this.$refs.assetFlow.applyFormData.id; });
你可以在`map`函数中添加一个`if`语句来判断当前`item`的`id`是否需要删除。具体的操作步骤如下:
1. 定义一个要删除的id,比如说`id_to_delete`。
2. 在`map`函数中添加一个`if`语句,判断当前`item`的`id`是否等于要删除的id。如果是,则跳过该项,不做任何处理。
```javascript
page.multipleSelection.map((item) => {
if (item.id === id_to_delete) {
return;
}
item.goodsId = item.id;
item.purchaseId = this.$refs.assetFlow.applyFormData.id;
});
```
完整的示例代码如下:
```javascript
// 定义要删除的id
const id_to_delete = 2;
// 遍历数组并删除指定id的项
page.multipleSelection.map((item) => {
if (item.id === id_to_delete) {
return;
}
item.goodsId = item.id;
item.purchaseId = this.$refs.assetFlow.applyFormData.id;
});
```
注意,这里假设`page.multipleSelection`是一个数组类型,如果是一个其他类型的数据结构,可能需要使用不同的遍历方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)