【发布时间】:2021-11-09 12:03:14
【问题描述】:
我有一个选定的商店对象数据。当我发出一个POST 请求作为返回时,我得到单个数据对象或数据数组,这取决于邮政编码。一个邮政编码有多个PickUp 点或单个PickUp 点。
我想根据POST请求返回数据创建一个Helper函数,如果它与我已经选择的商店对象数据不匹配,则返回true else return else。
我不知道如何创建不可预测的辅助函数,它可以是单个对象或具有多个对象的数组。
这是我的示例数据。我想和areaId比较数据
const selectedArea = {
areaId: "84ed84ad-81e3-49ed-absjsjjjs",
name: "New york restaurant",
address: {
city: "New York",
postalCode: "0012231",
street: "New jersy"
}
};
const newArea = [
{
areaId: "84ed84ad-81e3-49ed-absyay6w",
name: "Veg retaurant",
address: {
city: "Calfornia",
postalCode: "0018383",
street: "Calfornia"
}
},
{
areaId: "84ed84ad-sjsjjs",
name: "Indian restaurant",
address: {
city: "Calfornia",
postalCode: "001aas8383",
street: "Calfornia"
}
},
{
areaId: "84ed84ad-81e3-49sjsjjshq8q",
name: "Desi restaurant",
address: {
city: "Calfornia",
postalCode: "0011881",
street: "Calfornia"
}
},
{
areaId: "84ed84ad-sjsjj-ssks-msms",
name: "chinese restaurant",
address: {
city: "Calfornia",
postalCode: "02992",
street: "Calfornia"
}
}
];
const newArea =
{
areaId: "84ed84ad-81e3-49ed-absyay6w",
name: "Veg retaurant",
address: {
city: "Calfornia",
postalCode: "0018383",
street: "Calfornia"
}
}
;
const selectedAreaMatch = (selectedArea, newArea) => {
console.log({ selectedArea });
console.log({ newArea }); // it can be object or arrays
// if does not match return true
// if match return false
return true;
};
【问题讨论】:
-
您可以在进行比较之前将单个对象转换为数组。但是,比较可能有点棘手。是否需要比较对象中的所有字段来判断它们是否相同?
-
areaId是对象的唯一标识,还是多个对象可以有相同的areaId?
标签: javascript arrays object