gpt4 book ai didi

javascript - Lodash/Javascript 比较数组或对象,如果任何 Prop 匹配则失败

转载 作者:行者123 更新时间:2023-11-30 19:21:52 25 4
gpt4 key购买 nike

最好使用 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(),但不确定如何检查两者之间的每个属性。

让结果 = _.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>

关于javascript - Lodash/Javascript 比较数组或对象,如果任何 Prop 匹配则失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57363333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com