我一直在寻找一种方法来做到这一点,但似乎找不到任何东西,我有不同的配置对象,我需要将它们保存为变量中的文本以供稍后处理,这是一个示例:
目的:
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');
}'
}
Run Code Online (Sandbox Code Playgroud)
我需要将其保存为字符串,例如:
args.config.config = "{object goes here}";
Run Code Online (Sandbox Code Playgroud)
不要将所有内容都放在一个巨大的行上或添加换行符,因为稍后将对其进行解析以在配置中使用,这会使事情变得一团糟,有什么想法吗?
更新: 因此将它们更改为文本可能不是最好的解决方案,这些配置将存储在 mongo 数据库中,因此可以按原样使用它们(我还没有尝试过)。
我遇到的其他问题之一是在配置对象中我有这个:
final.push(item)
Run Code Online (Sandbox Code Playgroud)
和
return next(null, final)
Run Code Online (Sandbox Code Playgroud)
这将使用配置对象在另一个文件中定义:
其他文件:
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)
}
Run Code Online (Sandbox Code Playgroud)
所以 return next(null, final) 和 final.push(result) 必须引用新文件中的 var / 函数,但我不知道如何让它工作,这不是我必须添加的原因配置对象中的最终数组和一个空的 next 函数,然后像这样分配它:
exports.parse = function(args, next){
args.config.next = next;
....
Baby.parse(data, args.config)
}
Run Code Online (Sandbox Code Playgroud)
对象用丑陋的行调用它:
return args.config.config.next(null, args.config.config.final);
Run Code Online (Sandbox Code Playgroud)
如果有人有办法解决这个问题,将不胜感激。
如果您使用
JSON.stringify“replacer”功能和JSON.parse“reviver”功能以及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);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3108 次 |
| 最近记录: |