我有一個對象,它包含每個產品變體的配置,基本上我希望能夠搜索哪個對象具有2個特定值,然後獲取它的v_id值,只有1個結果。包含2個值的對象的搜索對象?
代碼:
//object
product_1: {
p_id: 11, // Pendant Conicol
variants: {
v_1: { v_id:397, color:"gold", size:"20|30" },
v_2: { v_id:396, color:"gold", size:"20|40" },
v_3: { v_id:395, color:"gold", size:"20|50" },
v_4: { v_id:394, color:"gold", size:"25|25" }
}
}
getVariantId: function() {
var results = []; // There will only be 1 result so maybe dont need array?
var searchFor = "gold and 20|50";
//Loop here?
console.log(results.v_id);
}
我怎樣才能返回對象我不斷收到一個未定義使用的console.log(results.v_id)在v_id; – Elevant
結果返回一個數組...結果[0] .v_id – sahbeewah