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]

  • 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` – Moritz Roessler Sep 13 '13 at 13:23

2 Answers2

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 ++;
}
Sergey Rura
  • 234
  • 2
  • 3
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]));
    }
};
Hippocrates
  • 2,510
  • 1
  • 19
  • 35
  • 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 – user2776546 Mar 10 '18 at 18:45