Vedendo come se le uniche risposte utili sono stati riferimento a librerie di terze parti -, ecco la soluzione javascript nativo. Per tutti quelli che vogliono solo poche righe di codice, piuttosto che una pila:
La funzione:
Array.prototype.findValue = function(name, value){
var array = map(this, function(v,i){
var haystack = v[name];
var needle = new RegExp(value);
// check for string in haystack
// return the matched item if true, or null otherwise
return needle.test(haystack) ? v : null;
});
return array;
}
Originario .map()
funzione:
map = function(array, mapFunction) {
var newArray = new Array(array.length);
for(var i = 0; i < array.length; i++) {
newArray[i] = mapFunction(array[i]);
}
return newArray;
}
vostro alloggio:
(scremato dall'abject pubblicato):
myObject = {
name : "soccer",
elems : [
{name : "FC Barcelona"},
{name : "Liverpool FC"}
]
},
{
name : "basketball",
elems : [
{name : "Dallas Mavericks"}
]
}
Per l'utilizzo:
(Questo cercherà l'array myObject.elems per un 'nome' Matching 'FC')
var matched = myObject.elems.findValue('name', 'FC');
console.log(matched);
Il risultato - controllare la console:
[Object, Object, findValue: function]
0: Object
name: "FC Barcelona"
__proto__: Object
1: Object
name: "Liverpool FC"
__proto__: Object
length: 2
__proto__: Array[0]
fonte
2014-06-09 16:16:29
Cosa è un oggetto JSON? Vuoi dire che hai una stringa JSON * *? –
Ha un oggetto JavaScript, ovviamente. Presumo che abbia detto che è JSON perché ha chiesto a un server come JSON o JSONP. :-) – Nosredna