【发布时间】: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