0

is there a way to make a function that runs console.log for a string of variables. running once for the name of the variable name and once to show its val

function multilog(text){
    var text=text.split(",")
    for(i=0;i<text.length;i++){
        console.log(text[i]+': ')
        console.log(JSON.stringify(text[i]));
    }
}
multilog('number_words,number_paragraphs,relatedwords');

an example of desired output

number_words: 1 number_paragraphs: 2 relatedwords: [example example example]

1
  • Using bracket notation [], if the variables are members of an object e.g. a global variable is a member of the window object window["number_words"] //1. If not you wold have to use eval Commented Sep 13, 2013 at 13:23

2 Answers 2

1
var logNumber = 0;

// The _log() will be a global function so you could access it from any place in the code.
// You can pass parameters like : _log( 'Something', 'Anything', '...', '...' ) 
_log = function () {
    console.log( '========= Log number : ' + logNumber + '. =========' )
    for( var i = 0, l = arguments.length; i < l; i ++ ) {
        console.log( arguments[ i ] );
    }
    console.log( '====== End of log number: ' + logNumber + '. ======' )
    logNumber ++;
}
Sign up to request clarification or add additional context in comments.

Comments

0
console.multilog = function(str){
    var toLog = str.split(',');
    for (var i = 0, len = toLog.length; i < len; i++){
    console.log(toLog[i] + ' =>');
    console.log(eval(toLog[i]));
    }
};

1 Comment

Thanks mate. I have used this a few times and think there are two tweaks that make it better for the real world application. 1) lowercase the L in multiLog so the function name is multilog. I have got confused a couple of times. 2) replace the console.log(toLog[i]); to console.log(toLog[i] + ' =>'); It just makes it a bit clearer when debugging

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.