我想用物件陣列檢查整個原始物件。我不想使用任何屬性名稱進行比較。
const items = [{ a: 1 },{ a: 2 },{ a: 3 }];
const item1 = { a: 2 };
如果我想item1在items不使用任何屬性但使用
整個物件的情況下檢查陣列中 是否存在該怎么辦。
indexOf filter some甚至lopping在這種情況下不起作用。
在這方面需要幫助。如何檢查?
順便說一下,我的情況不同。我有一個很長的物件,它的唯一性取決于很多屬性,這就是為什么我不想使用屬性進行比較。
我有這樣的事情。
function bigArraySol() {
const bigArray = [
{
product_id: "1",
product_name: "Womens Achara",
total_price: "8000",
unique_product_id: "1",
seller_id: "sell1",
image: "https://i.imgur.com/6CsDtqD.jpg",
quantity: 3,
product_types: [
{
product_type_id: "1",
product_type_name: "Achara Cloth",
attributes: [
{
attribute_name: "Achara Length",
attribute_value: "4"
},
{
attribute_name: "Achara Cloth",
attribute_value: "Terry Cotton"
}
]
},
{
product_type_id: "2",
product_type_name: "Khadki ",
attributes: [
{
attribute_name: "Khadki Cloth",
attribute_value: "Ready Made"
},
{
attribute_name: "khadki Color",
attribute_value: "mix"
}
]
},
{
product_type_id: "3",
product_type_name: "Blouse",
attributes: [
{
attribute_name: "Blouse Size",
attribute_value: "20"
}
]
}
]
},
{
product_id: "1",
product_name: "Womens Achara",
total_price: "8000",
unique_product_id: "1",
seller_id: "sell1",
image: "https://i.imgur.com/6CsDtqD.jpg",
quantity: 3,
product_types: [
{
product_type_id: "1",
product_type_name: "Achara Cloth",
attributes: [
{
attribute_name: "Achara Length",
attribute_value: "4"
},
{
attribute_name: "Achara Cloth",
attribute_value: "Terry Cotton"
}
]
},
{
product_type_id: "2",
product_type_name: "Khadki ",
attributes: [
{
attribute_name: "Khadki Cloth",
attribute_value: "Ready Made"
},
{
attribute_name: "khadki Color",
attribute_value: "mix"
}
]
},
{
product_type_id: "3",
product_type_name: "Blouse",
attributes: [
{
attribute_name: "Blouse Size",
attribute_value: "25"
}
]
}
]
},
{
product_id: "1",
product_name: "Womens Achara",
total_price: "8000",
unique_product_id: "1",
seller_id: "sell1",
image: "https://i.imgur.com/6CsDtqD.jpg",
quantity: 3,
product_types: [
{
product_type_id: "1",
product_type_name: "Achara Cloth",
attributes: [
{
attribute_name: "Achara Length",
attribute_value: "4"
},
{
attribute_name: "Achara Cloth",
attribute_value: "Terry Cotton"
}
]
},
{
product_type_id: "2",
product_type_name: "Khadki ",
attributes: [
{
attribute_name: "Khadki Cloth",
attribute_value: "Ready Made"
},
{
attribute_name: "khadki Color",
attribute_value: "mix"
}
]
},
{
product_type_id: "3",
product_type_name: "Blouse",
attributes: [
{
attribute_name: "Blouse Size",
attribute_value: "25"
}
]
}
]
}
];
const compairingObj = {
product_id: "1",
product_name: "Womens Achara",
total_price: "8000",
unique_product_id: "1",
seller_id: "sell1",
image: "https://i.imgur.com/6CsDtqD.jpg",
quantity: 3,
product_types: [
{
product_type_id: "1",
product_type_name: "Achara Cloth",
attributes: [
{
attribute_name: "Achara Length",
attribute_value: "4"
},
{
attribute_name: "Achara Cloth",
attribute_value: "Terry Cotton"
}
]
},
{
product_type_id: "2",
product_type_name: "Khadki ",
attributes: [
{
attribute_name: "Khadki Cloth",
attribute_value: "Ready Made"
},
{
attribute_name: "khadki Color",
attribute_value: "mix"
}
]
},
{
product_type_id: "3",
product_type_name: "Blouse",
attributes: [
{
attribute_name: "Blouse Size",
attribute_value: "20"
}
]
}
]
};
}
在沙箱中查看:https : //codesandbox.io/s/javascript-forked-q8yu6? file =/ index.js: 0-4933
uj5u.com熱心網友回復:
您可以簡單地 JSON 字串化它們并進行比較。
const items = [{ a: 1 },{ a: 2 },{ a: 3 }];
const item1 = { a: 2 };
items.map((item, index) => {
if (JSON.stringify(item) === JSON.stringify(item1)) {
console.log('item1 exists in index ' index);
}
})
uj5u.com熱心網友回復:
此代碼使用回圈并查看屬性來實作“陣列中的物件 indexOf”。它非常簡單,并且將匹配物件的haystack每個屬性具有相同值的任何needle物件。它應該很容易修改,以檢查匹配不具有的屬性沒有針發現-但我不能確定你的需求。
const indexOf = (needle, haystack) => {
let index = -1;
haystack.every((o,i) => {
let found = true;
for (let [k,v] of Object.entries(needle)) {
if (o[k] !== v) { found = false; break; }
}
if (found) index=i;
return !found; // false exits
});
return index;
}
const haystack = [{ a: 1 },{ a: 2 },{ a: 3 }];
const needle = { a: 2 };
console.log(indexOf(needle, haystack)); // returns 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377965.html
標籤:javascript 数组 目的
上一篇:從鍵不在陣列中的物件中洗掉條目
下一篇:如何使用jq連接兄弟陣列?
