1

我有一个像下面这样的对象,我想根据“96f54547-767c-434b-bcb4-a239a36b1c56”搜索/查找元素并获得一个数组[“x”,“y”]。如何做到这一点?

<script type="text/javascript">
var cList = {
    "96f54547-767c-434b-bcb4-a239a36b1c56": ["x","y"],
    "fd3f9224-9fa5-49f5-9eea-ffd0ff40fdb0": [null,"y"],
    "843ed981-979f-4639-be6d-93665e52246f": [null,"y"],
    "2208ca60-c0d1-4ee9-aaae-291bef9622fa": [null,"y"]
};     
</script>
4

2 回答 2

6

You can use bracket operators to find the Array:

var values = cList["96f54547-767c-434b-bcb4-a239a36b1c56"];

Either hard-coded as above or with the key stored in another variable:

var guid = "96f54547-767c-434b-bcb4-a239a36b1c56";
var values = cList[guid];

Also, if you want to test whether the Object even has the key, you can use the in keyword:

if (!(guid in cList)) {
    throw new Error('Data does not include the expected GUID: ' + guid);
}
于 2013-06-30T06:02:33.753 回答
1

你可以试试这样

 var value= cList["96f54547-767c-434b-bcb4-a239a36b1c56"]
于 2013-06-30T06:20:19.160 回答