【问题标题】:Lodash/Javascript Compare array or objects, fail if any props matchLodash/Javascript 比较数组或对象,如果任何道具匹配则失败
【发布时间】:2019-08-05 17:22:02
【问题描述】:

最好使用 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"
      }
    ]
  }
]

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)

【问题讨论】:

    标签: javascript arrays lodash


    【解决方案1】:

    你可以使用_.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>

    【讨论】:

    • 到目前为止运行良好......但是假设我在每个数组中都有一个名为“componentZ”的常量对象。在检查其他所有内容时如何忽略那个?
    • @jobiin 您可以根据需要调整 customizer 函数,并从中相应地返回值,即,由于您想忽略具有某些值的对象,因此您可以在 coutomizer 中测试这些值,类似于我们在这里为名字做的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多