我一直在寻找一种方法来做到这一点,但似乎找不到任何东西,我有不同的配置对象,我需要将它们保存为变量中的文本,以便稍后进行一些处理,这是一个示例:
对象:
args.config.config = {
next: null,
final:[],
delimiter: '~', header: false,
step: function (row) {
var item = {
'line_code': row.data[0][0],
'order': row.data[0][1]
}
args.config.config.final.push(item);
},
complete: function (result) {
console.log('Reading data completed. Processing.');
return args.config.config.next(null, args.config.config.final);
},
error: function () {
console.log('There was an error parsing');
}'
}
我需要将其保存为字符串,例如:
args.config.config = "{object goes here}";
如果不将所有内容都放在一个巨大的行上或添加换行符,因为稍后将对其进行解析以在配置中使用,这会把事情搞砸,有什么想法吗?
更新: 所以把它们变成文本可能不是最好的解决方案,这些配置将存储在一个mongo数据库中,所以它可能会按原样使用它们(我还没有尝试过)。
我遇到的另一个问题是在配置对象中我有这个:
final.push(item)
和
return next(null, final)
将使用配置对象在另一个文件中定义:
其他文件:
exports.parse = function(args, next){//next is what I need to call in the config
var final = []; //this is the final referred to in the config object
....
Baby.parse(data, args.config)
}
因此 return next(null, final) 和 final.push(result) 必须引用新文件中的 var/函数,但我不知道如何让它工作,这就是我有在配置对象中添加一个最终数组和一个 null next 函数,然后像这样分配它:
exports.parse = function(args, next){
args.config.next = next;
....
Baby.parse(data, args.config)
}
对象用丑陋的一行调用它:
return args.config.config.next(null, args.config.config.final);
如果有人能解决这个问题,我们将不胜感激。
最佳答案
如果你使用 JSON.stringify
with a "replacer" function 和
JSON.parse
with a "reviver" function连同 new Function()
,你可以做到:
我不确定我是否在关注您提出的第二个(更新的)问题。将对象解析回对象后,为什么不能在调用对象的任何方法之前将 next
和 final
属性初始化为有效对象?您甚至可以在该方法中添加测试,在返回任何内容之前检查 final
和 next
是否存在。
var myObj = {
next: null,
final:[],
delimiter: '~',
header: false,
step: function (row) {
var item = {
'line_code': row.data[0][0],
'order': row.data[0][1]
};
args.config.config.final.push(item);
},
complete: function (result) {
console.log('Reading data completed. Processing.');
return args.config.config.next(null, args.config.config.final);
},
error: function () {
console.log('There was an error parsing');
}
};
// Stringify the object using a replacer function that will explicitly
// turn functions into strings
var myObjString = JSON.stringify(myObj, function(key, val) {
return (typeof val === 'function') ? '' + val : val;
});
// Now, parse back into an object with a reviver function to
// test for function values and create new functions from them:
var obj = JSON.parse(myObjString, function(key, val){
// Make sure the current value is not null (is a string)
// and that the first characters are "function"
if(typeof val === "string" && val.indexOf('function') === 0){
// Isolate the argument names list
var start = val.indexOf("(") + 1;
var end = val.indexOf(")");
var argListString = val.substring(start,end).split(",");
// Isolate the body of the function
var body = val.substr(val.indexOf("{"), val.length - end + 1);
// Construct a new function using the argument names and body
// stored in the string:
return new Function(argListString, body);
} else {
// Non-function property, just return the value
return val;
}
}
);
// Test the method:
obj.error(); // 'There was an error parsing' is written to console.
// Examine the object:
console.log(obj);
关于JavaScript - 使用方法将对象保存为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40875630/