首先需要兩個(gè)數(shù)組,這里直接致敬下Element-ui的table數(shù)據(jù)
tableData: [
{ date: '2016-05-02', name: '王小鼠', address: '上海市普陀區(qū)金沙江路 1518 弄' },
{ date: '2016-05-04', name: '王小牛', address: '上海市普陀區(qū)金沙江路 1517 弄' },
{ date: '2016-05-01', name: '王小虎', address: '上海市普陀區(qū)金沙江路 1519 弄' },
{ date: '2016-05-03', name: '王小羊', address: '上海市普陀區(qū)金沙江路 1516 弄' }
]
dataTable: [
{ date: '2016-05-02', name: '王小龍', address: '上海市普陀區(qū)金沙江路 1518 弄' },
{ date: '2016-05-04', name: '王小蛇', address: '上海市普陀區(qū)金沙江路 1517 弄' },
{ date: '2016-05-01', name: '王小虎', address: '上海市普陀區(qū)金沙江路 1519 弄' },
{ date: '2016-05-03', name: '王小羊', address: '上海市普陀區(qū)金沙江路 1516 弄' }
]
之后使用filter和every 對(duì)比出第一個(gè)數(shù)組里name與第二個(gè)不相同的部分
let arrTools = tableData.filter( filItem => dataTable.every(eveItem => !Object.is(filItem.name,eveItem.name)))
arrTools:[
{
"date": "2016-05-02",
"name": "王小鼠",
"address": "上海市普陀區(qū)金沙江路 1518 弄"
},
{
"date": "2016-05-04",
"name": "王小牛",
"address": "上海市普陀區(qū)金沙江路 1517 弄"
}
]
//取出了數(shù)組tableData中與dataTable中不相等的部分
這里使用了ES6的filter、every、Object.is()來實(shí)現(xiàn)求數(shù)組的補(bǔ)集
為了方便大家觀看,形參使用了應(yīng)該能更清晰的寫法