简体   繁体   English

将对象(数组?)转换为字符串

[英]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. 它包含一个javascript对象(数组?),我试图将其内容转换为字符串。 As I believe it may include some data I need. 我相信其中可能包含一些我需要的数据。

The object is called submitDataDisplay. 该对象称为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. 我尝试使用JSON.stringify(obj)来查看内容,但这不能正常工作,因为它无法获取所有值,因为我无法在循环期间将相关行项目添加到命令中。

This is from the html file: 这是来自html文件:

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: 这来自js文件,我认为这是相关的部分:

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. 它包括一个InnerText条目,其中包含我需要的所有数据。

$submitDataDislay is neither an object now an array, it is a DOM node representing an HTML element on your web page, fetched by jQuery. $submitDataDislay既不是对象,也不是数组,它是一个DOM节点,表示您网页上的HTML元素,由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. 您发布的代码从名为sheetStates的数组读取数据,将其内容写入DOM节点,以便在浏览器中显示。

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. 这会将数据打印到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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