2016-02-05 115 views
1

JSON對象具有「name:value」關係。 我需要一種方法來返回一個對象名稱,而不是它的值。JSON如何返回對象名稱而不是其值

例如,如果動物[0] .species會返回值「貓科動物」, 我該如何返回名稱「物種」?

我寫的函數要求我在返回該對象的值之前檢查一個對象名是否等於 函數的輸入。

我已搜查計算器和W3Schools的一個解決方案,但沒有成功。

var animals = [ 
{ 
    "animal": "cat", 
    "species": "feline", 
    "likes": "yarn", 
    "dislikes" : "water" 
}, 
{ 
    "animal": "dog", 
    "species": "canine", 
    "likes": "rubber balls", 
    "dislikes": "thunder" 
} 
]; 


function lookUp(property){ 
    if (animals[0].property == "species") { 
    return animals[0].species; 
    } 
} 
lookUp("species"); 
+0

你是否有意檢查屬性是否存在,如果是的話返回它的值? –

+0

這正是我的意思。 – Naethanyel

+0

應該注意的是,您的代碼中已定義了一組對象。這不應該被稱爲JSON。 – alex

回答

3

你錯過了引號species

function lookUp(property) { 
    if (animals[0].property === 'species') { // see the quotes ? 
    return animals[0].species; 
    } 
} 

該函數使用animals自由變量,而不是非常有用的你,如果你對任何其他物體上使用lookUp曾經計劃。通過不同的寫入功能,你可以把它更加靈活

function lookUp(object, property) { 
    if (object.hasOwnProperty(property)) { 
    return object[property]; 
    } 
    return null; 
} 

現在看一個具體的動物

lookUp(animals[0], 'species'); // => 'feline' 
lookUp(animals[1], 'species'); // => 'canine' 
lookUp(animals[2], 'species'); // => null 

你可以藉此一步通過定義高階程序

function getSpecies(animal) { 
    return lookUp(animal, 'species'); 
} 

getSpecies(animal[0]); // => 'feline' 
getSpecies(animal[1]); // => 'canine' 
getSpecies(animal[2]); // => null 

每Alex的評論,在過去的一部分,你可以使用partial function application或你可以currylookUp函數。

const lookUp = x => y => y.hasOwnProperty(x) ? y[x] : null; 
const species = lookUp('species'); 
species(animal[0]); // => 'feline' 
species(animal[1]); // => 'canine' 
species(animal[2]); // => null 
+0

一旦OP得到它的掛起,他們可以使用通用的部分包裝函數來實現底層函數。 – alex

4

您可以看到屬性是否存在於對象上,如果存在,就返回它。

function lookUp(property){ 
    if (animals[0].hasOwnProperty(property)) { 
    return animals[0][property]; 
    } 
} 

你可以使用in太多,但它會走原型鏈尋找物業。有時這是你想要的,但通常不是。我會從你的代碼中猜測這不是你的意圖。

使用hasOwnProperty()確保財產是直接提供原型鏈上的。如果你擔心hasOwnProperty可能是一個有效的屬性名稱(如在這裏),然後使用Object.prototype.hasOwnProperty.call(animals[0], property)。這將使用該功能的安全參考。否則,JavaScript會認爲你的意圖是嘗試使用該屬性執行對象上的值(這可能是「somestring」不是函數例外)。

+0

如果你擔心''hasOwnProperty'超過'in',你應該擔心'hasOwnProperty'屬性以及... – Bergi

+0

@Bergi絕大多數情況下。 OP似乎在學習,所以我想盡量讓它更容易。我將詳細說明最後一段。 – alex

+0

我只是想'如果(動物中的財產[0])'會更容易 – Bergi

0

下面是可以採取對象的數組或一個對象作爲其第一個參數和屬性查找作爲第二個參數的自適應查找功能。

var animals = [{ 
    "animal": "cat", 
    "species": "feline", 
    "likes": "yarn", 
    "dislikes": "water" 
}, { 
    "animal": "dog", 
    "species": "canine", 
    "likes": "rubber balls", 
    "dislikes": "thunder" 
}, { 
    "animal": "dog", 
    "species": "canine", 
    "likes": "rubber balls", 
    "dislikes": "thunder" 
}]; 


function lookUp (object, property) { 
    // check for array 
    if (object.constructor === Array) { 
     // loop through, find values and return them 
     var values = []; 
     object.forEach(function (value, key) { 
      if (property in value) { 
       values.push(value[property]); 
      } 
     }); 
     return values; 
    } else { 
     // otherwise, check for a property and return the value 
     return (property in object) ? object[property] : false; 
    } 
} 

lookUp(animals, 'species');   // => [ "feline", "canine", "canine" ] 
lookUp(animals[0], 'species');  // => "feline" 
lookUp(animals, 'fakeProperty'); // => [] 
lookUp(animals[0], 'fakeProperty'); // => false 
1
var animal = '[{"animal":"cat","species":"feline","likes":"yarn","dislikes":"water"},{"animal": "dog","species": "canine","likes": "rubber balls","dislikes": "thunder"}]'; 

var animals = JSON.parse(animal); 

function myCheck(e) { 
    for (key in animals) { 
    for (keys in animals[key]) { 
     if (keys == e) { 
      console.log(keys); 
     } 
    } 
    } 
} 

myCheck('species'); 

以動態對象可以是。在上面的例子中,你可以遍歷並檢查你的密鑰是否是你想要的密鑰。然後記錄您的密鑰,或者您可以執行您想要的特定操作。希望這可以幫助

相關問題