0

I would like to the iterate the array in this object:

obj = {'2012-01-01':[1,2,5,8]}

I understand we can iterate like this:

   for(var i = 0;i < obj.length;i++){
    obj['2012-08-08'][i]
    }

But if it is not known the name of the key how we iterate the array?

Say I have a loop:

_.each(dates, function(date){
     //each date is like the above javascript object, how to  iterate the array?

})
2
  • 4
    That's an object, so for..in is what you should be using here. Commented Jun 11, 2013 at 5:05
  • for(property in obj) { if(obj.hasOwnProperty(property)) { //do stuff } } Commented Jun 11, 2013 at 5:06

3 Answers 3

1

Use the _.keys helper:

var obj = {'2012-01-01':[1,2,5,8]};

_.each(_.keys(obj), function(date) {

    console.log(date);
    // 2012-01-01

    console.log(obj[date]);
    // [1,2,5,8]
});
Sign up to request clarification or add additional context in comments.

Comments

1
for(property in obj) { 
  if( Object.prototype.toString.call(property) === '[object Array]' ) {
         //property is array 
     }
 }

Comments

0

What if you have two arrays of objects and you would like to 'align' these object arrays so that you can make sure each array's objects are in the order as the other array's? What if you don't know what keys and values any of the objects inside of the arrays contains... Much less what order they're even in?

So you need a 'WildCard Expression' for your [].filter, [].map, etc. How do you get a wild card expression?

var jux = (function(){
    'use strict';

    function wildExp(obj){
        var keysCrude = Object.keys(obj),
            keysA = ('a["' + keysCrude.join('"], a["') + '"]').split(', '),
            keysB = ('b["' + keysCrude.join('"], b["') + '"]').split(', '),
            keys = [].concat(keysA, keysB)
                .sort(function(a, b){  return a.substring(1, a.length) > b.substring(1, b.length); });
        var exp = keys.join('').split(']b').join('] > b').split(']a').join('] || a');
        return exp;
    }

    return {
        sort: wildExp
    };

})();

var sortKeys = {
    k: 'v',
    key: 'val',
    n: 'p',
    name: 'param'
};
var objArray = [
    {
        k: 'z',
        key: 'g',
        n: 'a',
        name: 'b'
    },
    {
        k: 'y',
        key: 'h',
        n: 'b',
        name: 't'
    },
    {
        k: 'x',
        key: 'o',
        n: 'a',
        name: 'c'
    }
];
var exp = jux.sort(sortKeys);

console.log('@juxSort Expression:', exp);
console.log('@juxSort:', objArray.sort(function(a, b){
    return eval(exp);
}));

You can also use this function over an iteration for each object to create a better collective expression for all of the keys in each of your objects, and then filter your array that way.

This is a small snippet from the API Juxtapose which I have almost complete, which does this, object equality with exemptions, object unities, and array condensation. If these are things you need or want for your project please comment and I'll make the lib accessible sooner than later.

Hope this helps! Happy coding :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.