0

I am trying to create a code in Node.JS that will get an array with values and will pass it to a function but without success..

This is my code:

var tmpArr = [];
tmpArr.push('"2015-04-27", "12345", "http://thispage.com/2.html", 1, 1, 0, 0');
tmpArr.push('"2015-04-25", "34567", "http://thispage.com/2.html", 0, 0, 1, 1');
tmpArr.push('"2015-04-25", "34567", "http://thispage.com/2.html", 0, 0, 1, 1');

function reportPages(arr) {
    for (i in arr){
        putPage(arr[i]); //this did not work
    }
}

function reportPages(arr) {
    for (i in arr){
        putPage(eval(arr[i])); //this did not work eater 
    }
}

reportPages(tmpArr) 

Thanks to all the helpers!

webin
  • 1

1 Answers1

1

Well, an arguments list is no structure that can be represented with plain js. To pass multiple function arguments that are encoded as a string, you'd have to use

eval('putPage('+arr[i]+')')

or better

putPage.apply(null, JSON.parse('['+arr[i]+']'));

However, your putPage function does not even expect multiple arguments but a single array, so you'd just have to use

putPage(JSON.parse('['+arr[i]+']'));

I would however recommend to store proper JSON strings or real arrays in your tmpArr in the first place, like

var tmpArr = [
    ["2015-04-27", "12345", "http://thispage.com/2.html", 1, 1, 0, 0],
    ["2015-04-25", "34567", "http://thispage.com/2.html", 0, 0, 1, 1],
    ["2015-04-25", "34567", "http://thispage.com/2.html", 0, 0, 1, 1]
];
Bergi
  • 630,263
  • 148
  • 957
  • 1,375