2016-07-15 54 views
2

好吧,我有一个记录集对象:唔明此更新功能的流动

var collection = { 
    "2548": { 
     "album": "Slippery When Wet", 
     "artist": "Bon Jovi", 
     "tracks": [ 
     "Let It Rock", 
     "You Give Love a Bad Name" 
     ] 
    }, 
    "2468": { 
     "album": "1999", 
     "artist": "Prince", 
     "tracks": [ 
     "1999", 
     "Little Red Corvette" 
     ] 
    }, 
    "1245": { 
     "artist": "Robert Palmer", 
     "tracks": [ ] 
    }, 
    "5439": { 
     "album": "ABBA Gold" 
    } 
}; 

的代码更新,与下面的下面的函数:

function updateRecords(id, prop, value){ 
    if (prop === "tracks" && value !== "") { 
    if(collection[id][prop]) { 
    collection[id][prop].push(value); 
    } 
    else { 
    collection[id][prop]=[value]; 
    } 
    } else if (value !== "") { 
    collection[id][prop] = value; 
    } else { 
    delete collection[id][prop]; 
    } 

    return collection; 

} 

我期待了解上述代码片段的功能。我读了它,就像这样:

if (prop === "tracks" && value !== "") { 
    if(collection[id][prop]) { 
    collection[id][prop].push(value); 
    } 

如果“托”等于轨道和值不为空,继续到功能的更新部分。下一部分,我不确定,似乎说'如果集合的ID和道具有价值,把所述ID和道具推入集合。

接下来的部分,我肯定就有点迷失:

else { 
    collection[id][prop]=[value]; 
    } 

与上面开始,我还不清楚,如果这个else语句的目的是只核实信息是否已经包含在阵列。

else if (value !== "") { 
    collection[id][prop] = value; 
    } else { 
    delete collection[id][prop]; 
    } 

最后,上面似乎测试只是值是空白的,如果不是,它会插入所述值到数组中。如果所有条件都失败,则id和prop不会添加到数组中。我觉得我没有办法解决这个问题,但是如果有人能够帮助澄清一些混淆,那将不胜感激。

回答

0

首先我解释一下,这是如何工作:

collection[id][prop]

你有一个这样的对象:

var collection = { 
    "ID_1": { 
    "PROPERTY_A": "AAA" 
    } 
} 

访问哪些是物业ID_1collectionPROPERTY_A内的对象您可以做到这一点:

collection["ID_1"]["PROPERTY_A"] // -> "AAA"

,你也可以设定值,这样

collection["ID_1"]["PROPERTY_A"] = "AAAAAAAAAAAAAAA"

OK,让我们检查代码:

// if the prop is "tracks", it should be an array, there are many tracks... 
// so it should be handled like an array 
if (prop === "tracks" && value !== "") { 

    // does "tracks" exists for this item? 
    if(collection[id][prop]) { 

    // yes.. then push it into the array 
    collection[id][prop].push(value); 

    } else { 
    // otherwise, (when "tracks" does not exist) 
    // create the array and put the value as first item 

    collection[id][prop] = [value]; 
    } 

上面的代码可以简化为这样:

// so it should be handled like an array 
if (prop === "tracks" && value !== "") { 
    // set it to the same value, if the value is not falsey (null, 0, undefined) or make it to an array.. 
    collection[id][prop] = collection[id][prop] || [];  
    // push the value to it 
    collection[id][prop].push(value) 

接下来的事情:

// prop is not "tracks", but could be an empty string "", check it 
else if (value !== "") { 
    // Ok, the value is not an empty string 
    // set the value to the prop 
    collection[id][prop] = value; 
} else { 
    // If this is an empty string == "", then delete this property 
    delete collection[id][prop]; 
} 

尝试去了解它,这是非常基础,如果你有一个特殊问题,请让它尽可能精确。

+0

这很有道理,只是一个简单的问题。代码中的[id] [prop] = value部分,这些部分是将测试应用到阵列的各个层次上吗?例如,ID = 2468,道具=“专辑”,“艺术家”,“曲目”,价值是我们正在申请的所有权? @webdeb – AnabolicHippo

+0

是的,但是当[[id] [“tracks”]是一个数组时,你应该做'[id] [“tracks”]。push(value) – webdeb