let a1 = [
	{id: 1,text: ''},
	{id: 2,text: ''},
	{id: 3,text: ''},
	{id: 4,text: ''},
	{id: 5,text: ''},
	{id: 6,text: ''},
	{id: 7,text: ''},
]	
let a2 = [
	{id: 1,text: ''},
	{id: 2,text: ''},
	{id: 3,text: ''}
]
let a3 = [
	{id: 3,text: ''},
	{id: 4,text: ''},
	{id: 5,text: ''},
]

let result = difObjectArray(a1, a2, a3)
console.log(result)

function difObjectArray (a1, a2, a3) {
	// 获取a1中不包含a2,a3的元素,a1,a2,a3所包含的字段可能不同,但都包含id

	// 取出各个数组的id
	let a1_key = a1.map((item) =>item.id)
	let a2_key = a2.map((item) =>item.id)
	let a3_key = a3.map((item) =>item.id)

	// 合并a2,a3
	let concat = [].concat(a2_key,a3_key)

	// 去重
	let noDuplicate = Array.from(new Set(concat))

	// 获取差值
	let diff = a1_key.filter(key => !noDuplicate.includes(key))

	// 获取筛选后的数组
	let resultArray = []
	let length = a1.length
	for (let i = 0;i < length; i++) {
	  let subLength = diff.length
	  for (let j = 0; j < subLength; j++) {
	    if (a1[i].id == diff[j]) {
	      resultArray.push(a1[i])
	    }
	  }
	}
	return resultArray
}	

作者 铁血 汉子 2019年7月22日
2024/11/02/07:26:52pm 2019/7/22/14:05:21
0 1345