关于前端:数组中删除一个或者多个数组对象

28次阅读

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

   var arr1 = [{a: "111", b:"222",c:1,d:6},
      {a:"333", b:"444",c:11,d:56},
      {a:"555", b: "666",c:21,d:46},
      {a: "777", b:"888",c:13,d:36},
      {a:"999", b:"000",c:14,d:26},
      {a:"110", b: "220",c:51,d:16}
    ]
    var arr2 = [{a:"333", b:"444",c:11,d:56},
      {a:"555", b: "666",c:21,d:46},
      {a: "777", b:"888",c:13,d:36},
      {a:"999", b:"000",c:14,d:26},
      
    ]
    function removeList(originList, delList) {
      var newArr = originList
      delList.forEach((Element1, index1) => {originList.forEach((Element2, index2) => {if (isObjectValueEqual(Element1, Element2)) {newArr.splice(index2, 1)
          }
        })
      })
      originList = newArr
      return originList
    }
    function isObjectValueEqual(a, b) {var aProps = Object.getOwnPropertyNames(a);
      var bProps = Object.getOwnPropertyNames(b);
      if (aProps.length != bProps.length) {return false;}
      for (var i = 0; i < aProps.length; i++) {var propName = aProps[i]
        var propA = a[propName]
        var propB = b[propName]
        if ((typeof (propA) === 'object')) {if (this.isObjectValueEqual(propA, propB)) {return true} else {return false}
        } else if (propA !== propB) {return false} else {}}
      return true
    }
    console.log(removeList(arr1,arr2));
// 不扭转原数组的状况
    console.log(removeList(JSON.stringify(arr1),arr2));

正文完
 0