简体   繁体   中英

Convert Object (Array?) To String

I have acquired some code but the developer in not contactable. It contains a javascript object (array?) that I am trying to turn its contents into a string. As I believe it may include some data I need.

The object is called submitDataDisplay. At the moment its content is ouput to the bottom of the page (I believe oit is this object that contains the data), which I can copy and paste but my aim is to save it to a text file, programmatically. I might be right off the ball here and this is not possible but I want to give it a shot.

I have tried using JSON.stringify(obj) to see the contents, but this does not work as it does not get all the values as I am unable to add the relevant row items into the command during the loop.

This is from the html file:

updateRemark(sheet);

$("#J_timingSubmit").click(function(ev){

    var sheetStates = sheet.getSheetStates();
    var rowsCount = dimensions[0];
    var $submitDataDisplay = $("#J_dataDisplay") ;

    $submitDataDisplay.html("<b>Raw Data Submitted:</b><br/>[<br/>");

    for(var row= 0, rowStates=[]; row<rowsCount; ++row){
        rowStates = sheetStates[row];
        $submitDataDisplay.append('&nbsp;&nbsp;[ '+rowStates+' ]'+(row==rowsCount-1?'':',')+'<br/>');

    }


    $submitDataDisplay.append(']');

This is from the js file, which I think is the relevant part:

initSheet();
eventBinding();


var publicAPI = {

    /*
     *  
     * @return : [[1,0,0,...,0,1],[1,0,0,...,0,1],...,[1,0,0,...,0,1]]
     * */
    getSheetStates : function(){
        return sheetModel.getSheetStates();
    },

    setRemark : function(row,html){
        if($.trim(html)!==''){
            $(thisSheet.find(".TimeSheet-row")[row]).find(".TimeSheet-remark").prop("title",html).html(html);
        }
    },

    getDefaultRemark : function(){
        return sheetOption.remarks.default;
    },

};

return publicAPI;

This is the data that gets posted to the bottom of the web page:

Raw Data Submitted:
[
  [ 1,1,0,1,0,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ],
  [ 1,1,1,1,1,1,1 ]
]

I have added to the last line of code:

    $submitDataDisplay.append(']');
    console.log(Object.values($submitDataDisplay));

This gives me some good data. It includes an InnerText entry that contains all the data I need.

$submitDataDislay is neither an object now an array, it is a DOM node representing an HTML element on your web page, fetched by jQuery.

The code you posted reads data from an array named sheetStates to write its content to the DOM node so that it is displayed in the browser.

If you want to store this data in a string instead, you can put it in a string variable, like this:

updateRemark(sheet);

$("#J_timingSubmit").click(function(ev){

    var sheetStates = sheet.getSheetStates();
    var rowsCount = dimensions[0];
    var data = "Raw Data Submitted:\n\n[\n";

    for(var row= 0, rowStates=[]; row<rowsCount; ++row){
        rowStates = sheetStates[row];
        data += '  [ '+rowStates+' ]'+(row==rowsCount-1?'':',')+'\n';

    }

    data += ']';

    console.log(data);
}

This will print the data to the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM