lodash/javaScript比较数组或对象,如果任何道具匹配,则失败



最好使用lodash,我如何比较两个对象,如果任何属性匹配,则返回false,而排除'name'。

array1 = [
  {
    "name": "componentA",
    "img": "www.url.com/image1.jpg"
  },
  {
    "name": "componentB",
    "header": "this is the default header",
    "text": "here is a default text post",
    "buttons": [{
      "title": "a button",
      "url": "http://www.url.com"
    }]
  },
  {
    "name": "componentB",
    "header": "this is the default header 2",
    "text": "here is a default text post 2 ",
    "buttons": [
      {
        "title": "a button 2",
        "url": "http://www.url2.com"
      },
      {
        "title": "a second button 2",
        "url": "http://www.url2_1.com"
      }
    ]
  }
]

vs

array2 = [
  {
    "name": "componentA",
    "img": "www.url.com/imageA.jpg"
  },
  {
    "name": "componentB",
    "header": "header changed",
    "text": "text post changed",
    "buttons": [{
      "title": "button changed",
      "url": "http://www.website.com"
    }]
  },
  {
    "name": "componentB",
    "header": "header 2 changed",
    "text": "here is a default text post 2 ",
    "buttons": [
      {
        "title": "button 2 changed",
        "url": "http://www.website2.com"
      },
      {
        "title": "button 2 changed again",
        "url": "http://www.website2_1.com"
      }
    ]
  },
]

您可以看到除了array2[2].text中的每个属性都发生了变化,这将导致错误。

目标是比较两个数组,并确保最终数组中都不存在默认占位符文本。如果存在任何默认的占位符文本,则不允许提交表格。每个对象都有一个name密钥,需要从检查中排除,因为它是呈现组件的原因。

首先使用_.isequal((,但不确定如何检查两者之间的每个属性。

let results = _.isEqual(array1, array2)

您可以使用_.isEqualWithcustomizer功能Loadash Ref

let array1 = [{"name": "componentA","img": "www.url.com/image1.jpg"},{"name": "componentB","header": "this is the default header","text": "here is a default text post","buttons": [{"title": "a button","url": "http://www.url.com"}]},{"name": "componentB","header": "this is the default header 2","text": "here is a default text post 2 ","buttons": [{"title": "a button 2","url": "http://www.url2.com"},{"title": "a second button 2","url": "http://www.url2_1.com"}]}]
let array2 = [{"name": "componentA","img": "www.url.com/imageA.jpg"},{"name": "componentB","header": "header changed","text": "text post changed","buttons": [{"title": "button changed","url": "http://www.website.com"}]},{"name": "componentB","header": "header 2 changed","text": "here is a default text post 2 ","buttons": [{"title": "button 2 changed","url": "http://www.website2.com"},{"title": "button 2 changed again","url": "http://www.website2_1.com"}]},]
let array3 = [{"name": "component3","img": "www.url.com/image1.jpg"},{"name": "componentB","header": "this is the default header","text": "here is a default text post","buttons": [{"title": "a button","url": "http://www.url.com"}]},{"name": "componentB","header": "this is the default header 2","text": "here is a default text post 2 ","buttons": [{"title": "a button 2","url": "http://www.url2.com"},{"title": "a second button 2","url": "http://www.url2_1.com"}]}]
function check(val1,val2,index){
 if(index === 'name') return true
}
console.log(_.isEqualWith(array1,array2,check))
console.log(_.isEqualWith(array1,array3,check))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

相关内容

最新更新