2016-07-15 137 views
1

我有一個對象。我想檢查一下是否存在特定的屬性。查找對象內的特定屬性

問題是:我正在查找的屬性可能在任何地方,即:對象的結構是不確定的。

例如:

obj1 = { "propIWant": "xyz" } 
obj2 = { "prop1": [ {"key": "value"}, {"key":"value"}, 1, {"key": { "propIWant": "xyz"}}] 

我試過以下,但似乎失敗:

var lastTry = function(entry){ 
    // if entry is an array 
    if(typeof entry === 'object' && entry instanceof Array){ 
    for(var i in entry) 
     entry[i] = this.lastTry(entry[i]); 
    } 
    // if entry is a normal object 
    else if(typeof entry === 'object'){ 
    // iterate through the properties of the entry 
    for(var key in entry){ 
     console.log('key is: ', entry[key]) 
     // in case the entry itself is an array 
     if(typeof entry[key] === 'object' && entry[key] instanceof Array){ 
     for(var i in entry[key]){ 
      entry[key][i] = this.lastTry(entry[key][i]); 
     } 
     } 
     // in case the entry is a simple object 
     else if(typeof entry[key] === 'object') { 
     console.log('entry[key] is an object', entry[key], key) 
     // if we directely find the property.. modify it 
     if(entry[key].hasOwnProperty('_internal_url')){ 
      **entry[key]['_internal_url'] = "http://localhost:4000"+entry[key]['_internal_url'];** <-- My objective 
     } 
     else{ 
      // call this method again on that part 
      // for(var i in entry[key]){ 
      // if(typeof entry[key][i] === 'object') 
      //  entry[key][i] = this.lastTry(entry[key][i]); 
      // } 
     } 
     }else{ 
     console.log('not found') 
     } 
    } 
    } 
} 

是否有人可以幫助我走出它,我發現:Find by key deep in a nested object但,而不是返回找到的部分,我想編輯該屬性並返回具有修改後的屬性的整個對象,而不僅僅是具有該屬性的對象的子集。

+0

@DominicTobias尋找到它 –

+0

的[在嵌套的JSON對象鍵深查找]可能的複製(http://stackoverflow.com/questions/15523514/find-by-key-deep-in-nested- json-object) – morels

回答

0

你試過:

obj1.hasOwnProperty('propIWant') 
0

如果你希望只檢查是否存在財產或沒有,你可以將字符串化對象,然後檢查是否值存在字符串或沒有。

var obj2 = { 
 
    "prop1": [{ 
 
     "key": "value" 
 
    }, { 
 
     "key": "value" 
 
    }, 
 
    1, { 
 
     "key": { 
 
     "propIWant": "xyz" 
 
     } 
 
    } 
 
    ] 
 
} 
 

 
var key = 'propIWant'; 
 
var key2 = 'propIWant1'; 
 

 
function isPropInObject(obj, prop) { 
 
    var r = new RegExp(prop + "\":") 
 
    var match = JSON.stringify(obj).match(r); 
 
    return match && match.length > 0 ? true : false 
 
} 
 

 
console.log(isPropInObject(obj2, key)) 
 
console.log(isPropInObject(obj2, key2))

+0

上面可能會產生一個問題:萬一對象是HTML元素輸出集合的值(假設文本字段的內容與我正在查找的內容相匹配,因此我最終可能會操縱它以及)。不錯的想法,但在這種情況下,它可能無法按預期工作 –

+0

然後,我想你應該更新你的問題。 – Rajesh

+0

經過適當考慮,我申請了數據串化和搜索。對象屬性深搜索返回具有該屬性的對象,即,我無法更改我正在查找的對象的屬性。感謝您的幫助:) –

0

好了,檢查的道具是對象本身,並使用遞歸函數來找到你正在尋找的「深」的財產?

function findProp(obj, lookFor) { 
    for (var prop in obj) { 
    if (prop == lookFor) return obj[prop] 
    if (typeof obj[prop] == 'object') { 
     var checkNested = findProp(obj[prop], lookFor)  
     if (checkNested) return checkNested 
    } 
    } 
    return false 
} 

它與console.log(findProp(obj2, 'propIWant'))

演示 - >http://jsfiddle.net/zqcurg70/

+0

謝謝!是否有可能操縱該屬性並返回整個對象?它不斷返回搜索值。我的想法是改變它的財產。再次感謝:) –

+0

@RamanathanIyer,如果我明白你的意思,那麼你可以返回一個對象像這樣更新的小提琴 - > ** http://jsfiddle.net/2cab85L3/** – davidkonrad

+1

@davidkonrad只是一個小指針,而是返回false作爲默認值,不返回任何東西或返回undefined。 'false'可以是一個有效的值。 – Rajesh

0

數組和對象數據可以是一種常見的方式訪問:

obj[key] and arr[pos];

這種方式可以簡化您的代碼。請注意,key可以是一個字符串,如果是數組,則可以是一個字符串。

以下版本僅在深度優先的邏輯中搜索元素和最終的子元素(包括數組和對象)。

var found = 0; 
var findProp = function(entry) { 

    if(typeof entry === 'object') 

    for(var key in entry) { 

     findProp(entry[key]); 

     if(entry[key].hasOwnProperty('_internal_url')) { 
     found++; 
     entry[key]['_internal_url'] = "http://localhost:4000" + entry[key]['_internal_url']; 
     } 
    } 
} 

console.log('found ' + found + 'occurrences');