Skip to content
Discussion options

You must be logged in to vote

js判读两个数组对象是否相同

`let arr1 = [{id: 1, name: '嘿嘿', go: 'xi', child: {id: 55}, is: 2}, 'hh']
let arr2 = [{id: 1, name: '嘿嘿', go: 'xi', child: {id: 55}, is: 2}, 'hh']

console.log(equalsObj(arr1, arr2))
console.log(equalsObj(JSON.stringify(arr1), JSON.stringify(arr2)))

/**

  • 深度比较两个对象是否相同
  • @param oldData 旧数据
  • @param newData 新数据
  • @returns {boolean}
    */
    function equalsObj(oldData, newData) {
    // 类型为基本类型时, 如果相同, 则返回trues
    if (oldData === newData) return true
    if (isObject(oldData) && isObject(newData) && Object.keys(oldData).length === Object.keys(newData).length) {
    // 类型为对象并且元素个数相同
    // 遍历所有对象中所有属性, 判断元素是否相同
    for (const key in oldData) {
    if (oldData.hasOwnProperty(key)) {
    if (!equalsObj(oldData[key], newDa…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by xiao-ice
Comment options

xiao-ice
Jul 11, 2022
Collaborator Author

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants