Array.prototype.slice, can't get 'slice' of undefined?

1.4k Views Asked by At

Ok so I have some code that brings data from a server in the form of an javascript array.

I have been looking into prototype and seen that you could select parts of the array using slice, but I can't seem to find out how it works even when looking at the exampled.

So far I have a module.js that loads the data and directs to my index page

$.ajaxSetup({
    data: {
        apptoken: apptoken
    }
});

var promise1 = $.get(dbidApplication, {
    a: "dbpage",
    pagename: "index.html"
});


var promise2 = $.get(dbidTable, {
    act: "API_GenResultsTable",
    query: "{3.EX." + kRid + "}",
    jsa: 1,
    options: "num-1",
});


$.when(promise1, promise2).then(function(templateArgs, dataArgs) {
    var template = templateArgs[0];
    var markup = Mustache.render(template, qdb_data);
    document.write(markup);
    $("#formContents").html(markup);
});

My index.html page is completely blank except for the

var stringit = qdb_data.toString();
var stationid = stringit.slice(4);

console.log(stationid);

on the console it shows my stringit and in 4 there is data, so I'm not sure what exactly I am doing wrong...All in all I am attempting to use the array to fill in a template.

This is what I am returned from my query earlier

var qdb_data = new Array();

 qdb_data[0] = new Array();
 qdb_data[0][0] = "VL14799- FT5850";
 qdb_data[0][1] = "05-13-2015";
 qdb_data[0][2] = "";
 qdb_data[0][3] = "TANK PROPANE";
 qdb_data[0][4] = "PLANT";
 qdb_data[0][5] = "PROPANE";
 qdb_data[0][6] = "TANK 31 PROPANE";
 qdb_data[0][7] = "PLANT";
 qdb_data[0][8] = "TANK 31 PROPANE";
 qdb_data[0][9] = "PROPANE";
 qdb_data[0][10] = "PROPANE";
 qdb_data[0][11] = "100 PSI";
 qdb_data[0][12] = "59 F";
 qdb_data[0][13] = "411";
 qdb_data[0][14] = "";
 qdb_data[0][15] = "";
 qdb_data[0][16] = "BR";
1

There are 1 best solutions below

0
On BEST ANSWER

In JavaScript both array and string have slice method.What you actually want is the array slice and what you have is string slice. JavaScript will perform the slice based on whether the variable is a string or array automatically.So in order to get the value at the 4th index do the following.

var stationid = qdb_data[0].slice(4,5);