I have a variable json that contains some json data. I am trying to retrieve a specific part of that data. I can do it using the index like shown here:
var newdata = json[listid].Taxonomies[0];
However I want to be able to retrieve the data using a name... For example I want to get the data where
json[listid].Taxonomies[?].InternalName = 'ABC'
and I don't know the value of '?'
Is there a way I can do this without doing an each loop on the taxonomies?
Some people suggested linqjs, and that may be OK if you need a lot of JSON querying. However, for this problem, you can use the Array.filter to specify which item to find. Obviously, there's still a loop under the hood.
var matches = json[listid].Taxonomies.filter(function(obj) {
return obj.InternalName === "ABC";
})
console.log(matches[0]);
http://jsfiddle.net/MC94q/
Note that if you are worried about performance, you should use an object instead of an array for your data representation, and you can key it by the property you'll be searching by.
A useful function I use if I need to search multiple times on an array is the following. This transforms the array into a map that can be accessed without loops. And if you're worried about performance, just generate the data in that format instead if you can.
/**
* Creates a map by the given property to allow fast searches
* @param {object[]} arr Array that we want to create a map from
* @param {string} keyProp The property to key by
* @param {boolean} [allowDuplicates] If this is true, the value
* of each key in the returned object will be an array with
* all matches
* @return A map keyed by the requested property. If the parameter
* allowDuplicates is falsy, key property collisions will
* overwrite the previous value. If the allowDuplicates is true,
* the map will have as its value an array of objects for the given key
*
* Example: Given the following array: arr=[{a:1, b:2}, {a:1, b:3}, {a:3, b:4)]
* The call to mapFromArray(arr, 'a') returns the following
* { 1: {a:1, b:3}, 3: {a:3, b:4} }
* Notice that {a:1,b:2} is not in the returned map because it has the
* same value in the key property 'a' as {a:1, b:3}, which wins out
* since it's later.
*
* Calling mapFromArray(arr, 'a', true) returns the following
* { 1: [{a:1, b:2}, {a:1, b:3}], 3: [{a:3, b:4}] }
*/
function mapFromArray(arr, keyProp, allowDuplicates) {
var map = {};
for (var i = 0, ln = arr.length; i < ln; i++) {
if (!allowDuplicates) {
// No duplicates allowed, may be overwriting an existing value
map[arr[i][keyProp]] = arr[i];
} else {
// Duplicates are allowed, create array of matching objects
// Ext.Array.push creates a one item array if its argument is
// not already an array, otherwise, it pushes and returns the array
map[arr[i][keyProp]] = Ext.Array.push(map[arr[i][keyProp]], arr[i]);
}
}
return map;
}
So say you wanted to search for taxonomies with InternalName of one of 'ABC' 'DEF' 'GHI', you would do the following:
var map = mapFromArray(json[listid].Taxonomies, "InternalName");
var ABC = map['ABC'], DEF = map['DEF'], GHI = map['GHI'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With