![](/img/trans.png)
[英]How to check if parent object contains child objects properties? Is there any way without converting object to array?
[英]How to check with whole raw object (without using any property) if that object contains in array of objects in javascript?
我想用對象數組檢查整個原始 object。 我不想使用任何屬性名稱進行比較。
const items = [{ a: 1 },{ a: 2 },{ a: 3 }];
const item1 = { a: 2 };
如果我想檢查item1
是否存在於items
數組中而不使用任何屬性但使用
整個 object。
indexOf
filter
some
甚至lopping
在這種情況下不起作用。
在這方面需要幫助。 如何檢查?
順便說一句,我的情況不同。 我有一個很長的 object 並且它的唯一性取決於許多屬性,這就是為什么我不想使用屬性進行比較。
我有這樣的東西。
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
您可以簡單地 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); } })
此代碼使用循環並查看屬性來實現“數組中的對象 indexOf”。 它非常簡單,並且將匹配haystack
中的任何 object,它對於needle
object 的每個屬性具有相同的值。 應該很容易修改以檢查匹配項是否沒有在針中找不到的屬性 - 但我不確定您的需求。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.