我有这个JSON文件,我正在使用TypeScript解析。在JSON.prase之后,我需要小写一些值。通过数组的整数索引更新属性时,一切正常,但是当我使用属性名字符串作为对象的索引时,该值不会在父对象中更新。
function printFields(obj) {
printLine(obj.stages[0].fieldScripts[0].preReq.field);
printLine(obj.stages[0].fieldScripts[0].fields[0]);
printLine(obj.stages[0].fieldScripts[0].optionalFields[0]);
}
function printLine(value) {
document.body.innerHTML += value + "<br/>";
}
function fieldsToLowerCase(script) {
forEachField({ in : script,
action: function(field, i, fields) {
fields[i] = field.toLowerCase();
}
});
}
function forEachField(info) {
var forEachFieldInFields = function(fields) {
if (!fields) {
return;
}
fields.some(function(s) {
printLine("Value before action: " + s.preReq.field);
var breakHasBeenCalled = false;
var breakableAction = function(f, i, array) {
breakHasBeenCalled = info.action(f, i, array);
return breakHasBeenCalled;
};
s.fields.some(breakableAction);
if (breakHasBeenCalled) {
return breakHasBeenCalled;
}
if (s.optionalFields) {
s.optionalFields.some(breakableAction);
return breakHasBeenCalled;
}
if (s.preReq) {
var a = breakableAction(s.preReq.field, "field", s.preReq);
printLine("Value after updated by Action: " + s.preReq.field);
return a;
}
return breakHasBeenCalled;
});
};
for (var _i = 0, _a = info.in.stages; _i < _a.length; _i++) {
var stage = _a[_i];
forEachFieldInFields(stage.fieldScripts);
}
}
function getObject() {
return {
"stages": [{
"stage": "Customer Info",
"fieldScripts": [{
"preReq": {
"field": "Commercial",
"value": "371060000"
},
"fields": [
"firstName",
"lastName"
],
"optionalFields": [
"middleName"
]
}]
}]
};
}
var obj = getObject();
printFields(obj);
fieldsToLowerCase(obj);
printLine("");
printFields(obj);
&#13;
答案 0 :(得分:0)
一旦我添加了一个可运行的脚本,我终于弄明白了:
if (s.optionalFields) {
s.optionalFields.some(breakableAction);
return breakHasBeenCalled;
}
应该看起来像
if (s.optionalFields) {
s.optionalFields.some(breakableAction);
if(breakHasBeenCalled){
return breakHasBeenCalled;
}
}