2016-01-20 106 views
2

我正在嘗試解決freeCodeCamp練習並且卡住了。練習的目標是:創建一個函數,查看對象數組(第一個參數),並返回具有匹配的屬性和值對(第二個參數)的所有對象的數組。源對象的每個屬性和值對必須存在於集合中的對象中(如果它將包含在返回的數組中)。在對象數組中搜索匹配的屬性和值對

所以我做了什麼,是做一個密鑰對的集合,另一個數組與密鑰對源。爲了查找匹配鍵,我嵌套for循環,如果找到這些鍵,則比較屬性。

但不知何故,我的代碼返回沒有匹配。

var collection = [{ 
    first: "Romeo", 
    last: "Montague" 
}, { 
    first: "Mercutio", 
    last: null 
}, { 
    first: "Tybalt", 
    last: "Capulet" 
}]; 
var source = { 
    last: "Capulet" 
}; 

var collectionKeys = []; 
for (var i = 0; i < collection.length; i++) { 
    collectionKeys.push(Object.keys(collection[i])); 
} 
var sourceKeys = Object.keys(source); 

//for every key pair 
for (var t = 0; t < collectionKeys.length; t++) { 
    //for every key in key pair 
    for (var x = 0; x < collectionKeys[t].length; x++) { 
    //for every key in search 
    for (var y = 0; y < sourceKeys.length; y++) { 
     //see if a key matches 
     if (sourceKeys[y] == collectionKeys[t][x]) { 
     //see if the value matches 
     if (collection[collectionKeys[t][x]] == source[sourceKeys[y]]) { 
      console.log(collection[t]); 
     } else { 
      console.log("value not found"); 
     } 
     } else { 
     console.log("key not found"); 
     } 
    } 
    } 
} 

有人能指出我做錯了什麼嗎?如果你想修補,我也創建了JSfiddle

+0

你會被更好的變量名稱所包圍,包括創建一些臨時變量以更好地表達正在處理的內容。例如,'sourcePropertyValue'比source [sourceKeys [y]]更容易閱讀。如果你已經瞭解了函數,那麼這個算法就會乞求分解成至少兩個函數。例如,我想要一個函數來確定兩個對象是否匹配。 –

回答

0

var collection = [{ 
 
    first: "Romeo", 
 
    last: "Montague" 
 
}, { 
 
    first: "Mercutio", 
 
    last: null 
 
}, { 
 
    first: "Tybalt", 
 
    last: "Capulet" 
 
}]; 
 
var source = { 
 
    last: "Capulet" 
 
}; 
 

 
var collectionKeys = []; 
 
for (var i = 0; i < collection.length; i++) { 
 
    collectionKeys.push(Object.keys(collection[i])); 
 
} 
 
var sourceKeys = Object.keys(source); 
 

 
//for every key pair 
 
for (var t = 0; t < collectionKeys.length; t++) { 
 
    //for every key in key pair 
 
    for (var x = 0; x < collectionKeys[t].length; x++) { 
 
    //for every key in search 
 
    for (var y = 0; y < sourceKeys.length; y++) { 
 
     //see if a key matches 
 
     if (sourceKeys[y] == collectionKeys[t][x]) { 
 
     if (collection[t][collectionKeys[t][x]] == source[sourceKeys[y]]) { 
 
     alert(collection[t].first+ " "+collection[t].last); 
 
     } else { 
 
      console.log("value not found"); 
 
     } 
 
     } else { 
 
     console.log("key not found"); 
 
     } 
 
    } 
 
    } 
 
}

變化collection[collectionKeys[t][x]]至收集[T] [collectionKeys [T] [X]] .. collection[collectionKeys[t][x]]給出控制檯undefined

+0

太多循環,更整潔的一個會很好。 –

+0

@HafizAllyLalani我只對OP的代碼做了一些改動.. – Dhara

2

在你的聲明更明確 - 幫助閱讀代碼更容易:

var sourceKeys = Object.keys(source), 
    i = 0, 
    j = 0, 
    collectionLength = collection.length, 
    sourceKeysLength = sourceKeys.length; 

while (i < collectionLength) { 
    j = 0; 
    while (j < sourceKeysLength) { 
     if (sourceKeys[j] in collection[i] && source[sourceKeys[j]] === collection[i][sourceKeys[j]]) { 
      console.log('found one!'); 
     } 
     j++; 
    } 
    i++; 
} 

https://jsfiddle.net/fullcrimp/1cyy8z64/

2

我還停留在這個有一個良好的時間,當我偶然發現了一些資源,以幫助。

我發現,而不是嵌套循環的混亂,我可以使用內置的循環方法來大大簡化我的代碼。

這裏是我發現我的解釋:

https://github.com/Rafase282/My-FreeCodeCamp-Code/wiki/Bonfire-Where-art-thou

function where(collection, source) { 
    var arr = []; 
    var keys = Object.keys(source); 
    // Filter array and remove the ones that do not have the keys from source. 
    arr = collection.filter(function(obj) { 
    //Use the Array method every() instead of a for loop to check for every key from source. 
    return keys.every(function(key) { 
     // Check if the object has the property and the same value. 
     return obj.hasOwnProperty(key) && obj[key] === source[key]; 
    }); 
    }); 

    return arr; 
} 
0

這是我來到了同樣的問題。

function whereAreYou(collection, source) { 
    // What's in a name? 

    // Only change code below this line 

    var arr = []; 
    var validObject; 

// check each object 
    for (var each_object in collection){ 
    validObject = true; 
    for (var key in source){ 
     if (collection[each_object].hasOwnProperty(key)){ 
     if (collection[each_object][key] != source[key]){ 
     // if no valid key 
     validObject = false; 
    } 
    } else { 
    // if no valid value 
    validObject = false; 
    } 
} 
    // otherwise, give it a green light 
if(validObject){ 
    arr.push(collection[each_object]); 
    } 
} 
return arr; 

} 
0

在這裏有一些洞察力,清晰的理解和較少的循環。

一些新的JavaScript功能,如一些過濾器,地圖真的很方便,使代碼更整潔。

function whatIsInAName(collection, source) { 
    // What's in a name? 
    var arr = []; 
    // Only change code below this line 
    collection.some(function(obj){ 
     var sk = Object.keys(source); //keys of source object 
     var sv = Object.values(source); //values of source object 
     var temp = 0; 
     for(i=0;i<sk.length;i++){ // run until the number of source properties length is reached. 
     if(obj.hasOwnProperty(sk[i]) && obj[sk[i]] === sv[i]){ // if it has the same properties and value as parent object from collection 
      temp++; //temp value is increased to track if it has matched all the properties in an object 
     } 
     } 
     if(sk.length === temp){ //if the number of iteration has matched the temp value 
     arr.push(obj); 
     temp = 0; // make temp zero so as to count for the another object from collection 
     } 
    }) 
    // Only change code above this line 
    return arr; 
}