首先需要两个数组,这里直接致敬下Element-ui的table数据
tableData: [
{ date: '2016-05-02', name: '王小鼠', address: '上海市普陀区金沙江路 1518 弄' },
{ date: '2016-05-04', name: '王小牛', address: '上海市普陀区金沙江路 1517 弄' },
{ date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' },
{ date: '2016-05-03', name: '王小兔', address: '上海市普陀区金沙江路 1516 弄' }
]
dataTable: [
{ date: '2016-05-02', name: '王小龙', address: '上海市普陀区金沙江路 1518 弄' },
{ date: '2016-05-04', name: '王小蛇', address: '上海市普陀区金沙江路 1517 弄' },
{ date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' },
{ date: '2016-05-03', name: '王小羊', address: '上海市普陀区金沙江路 1516 弄' }
]
之后使用filter和some 来筛选出数组内name相同的部分
let arrTools = tableData.filter( filItem => dataTable.some(somItem => Object.is(filItem.name,somItem.name)))
arrTools:[
{
"date": "2016-05-03",
"name": "王小虎",
"address": "上海市普陀区金沙江路 1516 弄"
}
]
这里使用了ES6的filter、some、Object.is()来实现求数组的并集
为了方便大家观看,形参使用了应该能更清晰的写法