关于javascript:JavaScript复杂对象数组比较收集不相等的对象

50次阅读

共计 1420 个字符,预计需要花费 4 分钟才能阅读完成。

const arr = [{ x: 0, y: 0, w: 4, h: 6, i: "ztqk", static: false, cmp: "Ztqk"},
  {x: 0, y: 1, w: 4, h: 6, i: "xmtzzcqk", static: false, cmp: "Xmtzzcqk"},
  {x: 0, y: 1, w: 4, h: 6, i: "xmtzzcqkAdcd", static: false, cmp: "XmtzzcqkAdcd"},
  {x: 0, y: 2, w: 4, h: 5, i: "xmtzwcqk", static: false, cmp: "Xmtzwcqk"},
  {x: 0, y: 2, w: 4, h: 5, i: "xmtzwcqkAdcd", static: false, cmp: "XmtzwcqkAdcd"},
  {x: 4, y: 0, w: 10, h: 12, i: "piemap", static: false, cmp: "Map"},
  {x: 4, y: 2, w: 10, h: 5, i: "zlcsssqk", static: false, cmp: "Zlcsssqk"},
  {x: 14, y: 0, w: 4, h: 5, i: "xmjdfb", static: false, cmp: "Xmjdfb"},
  {x: 14, y: 0, w: 4, h: 5, i: "xmjdfbAdcd", static: false, cmp: "XmjdfbAdcd"},
  {x: 14, y: 1, w: 4, h: 7, i: "xxhjg", static: false, cmp: "Xxhjg"},
  {x: 14, y: 1, w: 4, h: 7, i: "xxhjgTuban", static: false, cmp: "XxhjgTuban"},
  {x: 14, y: 2, w: 4, h: 5, i: "zlmjwcqk", static: false, cmp: "Zlmjwcqk"},
  {x: 14, y: 2, w: 4, h: 5, i: "zlmjwcqkAdcd", static: false, cmp: "ZlmjwcqkAdcd"},
]

const arr1 = [{ x: 0, y: 0, w: 4, h: 6, i: "ztqk", static: false, cmp: "Ztqk"},
  {x: 0, y: 1, w: 4, h: 6, i: "xmtzzcqk", static: false, cmp: "Xmtzzcqk"},
  {x: 0, y: 2, w: 4, h: 5, i: "xmtzwcqk", static: false, cmp: "Xmtzwcqk"},
  {x: 4, y: 0, w: 10, h: 12, i: "piemap", static: false, cmp: "Map"},
  {x: 4, y: 2, w: 10, h: 5, i: "zlcsssqk", static: false, cmp: "Zlcsssqk"},
  {x: 14, y: 0, w: 4, h: 5, i: "xmjdfb", static: false, cmp: "Xmjdfb"},
  {x: 14, y: 1, w: 4, h: 7, i: "xxhjg", static: false, cmp: "Xxhjg"},
  {x: 14, y: 2, w: 4, h: 5, i: "zlmjwcqk", static: false, cmp: "Zlmjwcqk"},
]

let obj = {}

for (let key of arr1) {obj[`${key.i}`] = '123'
}

for (let key of arr) {if (obj[key.i] !== '123') {console.log(key)
  }
}

要害剖析:
1.arr1 ∈ arr
2. 遍历 arr1,将每项的 id 设置为对象的 key
3. 遍历 arr,输出没有值的项

正文完
 0