0

My object looks like this:

aryControlDef = {
  'item1' : ['a','b','c'],
  'item2' : ['x','y','z']    
};

yet when I call this

var sAtt = 'z';
var vControlDef = Array.prototype.filter.call(aryControlDef, function(value) {return value[2] == sAtt});

I get this:

vControlDef = []

I know it's to do with the way array-like objects with keys iterate, because this loop gets the correct result:

for(var i in aryControlDef){
  if(aryControlDef[i][2]=='z'){
    vControlDef[aryControlDef[i][0]] = aryControlDef[i];
  };
};

what I'm after is a way to return an object containing the key(s) with attributes that match sAtt.

what I can't figure out is how to iterate the keys in the callback function, in the same way that is possible in the 'for...each' loop, and Google hasn't found me any results for this case use. TIA for any suggestions :)

-- ps. forgot to mention I am doing this in Google Apps Script which means no external libraries (and no DOM)

-- Suggested Solution 1

var vControlDef = Object.keys(aryControlDef).filter(function(key) {
  return aryControlDef[key][2] == sAtt;
});

raises a non-descript Google server error "We're sorry, a server error occurred. Please wait a bit and try again.".

Tim
  • 756
  • 1
  • 7
  • 12
  • What do you mean by "iterate the key in the callback function"? – Whymarrh Jul 13 '14 at 16:21
  • You're trying to invoke an array method on something that isn't an array. It's not even an "array-like object", it's just an object with no array-like characteristics. Of course that won't work. You can't use Array's methods to help you here. You're going to have to use a simple loop like the one you've already provided. – user229044 Jul 13 '14 at 16:21
  • 1
    Your object is not an "array-like object". In JavaScript, to be "array-like" means that your object has to have numerically-indexed properties, and a `.length` property that gives the upper bound on the indexes. – Pointy Jul 13 '14 at 16:23
  • thanks for the comments, my assumption was based on these cases - could you tell me what the distinction is? http://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes – Tim Jul 13 '14 at 16:41

1 Answers1

0

This may help.

var aryControlDef = {
  'item1' : ['a','b','c'],
  'item2' : ['x','y','z']    
};

var vControlDef = [];

for (key in aryControlDef) {
    if (aryControlDef.hasOwnProperty(key)) {
        // So now the possible value of
        // key is 'item1' / 'item2'
        // aryControlDef[key] is ['a','b','c'] / ['x','y','z']

        if (aryControlDef[key][2] == 'z') {
            vControlDef.push(aryControlDef[key]);
        }
    }
}

The result in vControlDef should be similar with what you think you would get from

var vControlDef = Array.prototype.filter.call(aryControlDef, function(value) {
    return value[2] == 'z'
});
Kenny Tang
  • 63
  • 1
  • 7
  • but I already have the loop solution as shown in my first post - I was hoping to use the native methods :) – Tim Jul 13 '14 at 16:47