我有一个有效的EcmaScript-3函数,可以将数组转换为特定的字符串格式。
数组:
ft: [
1: {name: "gender", value: "F", focus: 0, type: "radio"}
2: {name: "firstname", value: "empty", focus: 0, type: "text"}
3: {name: "lastname", value: "empty", focus: 0, type: "text"}
4: {name: "birthday", value: "empty", focus: 0, type: "text"}
5: {name: "street", value: "empty", focus: 0, type: "text"}
6: {name: "streetNo", value: "empty", focus: 0, type: "text"}
]
特定的字符串格式:
ft=gender.radio|F|0;firstName.text|empty|1;lastName.text|empty|0;street.text|empty|0;houseNumber.text|empty|0;zip.text|empty|0;city.text|empty|0;country.select-one|de|0;birthdate-day.text|empty|0;birthdate-month.text|empty|0;birthdate-year.text|empty|0;email.email|empty|0;code.text|filled_out|0
我的将数组转换为特定字符串格式的工作函数:
var arr = [];
for (var i = 0; i < ft.length; i++) {
var elem = ft[i];
arr.push(elem.name + "." + elem.type + "|" + elem.value + "|" + elem.focus);
}
var str = arr.join(";");
console.log(str);
问题:
我需要一个可以像这样调用的函数(EcmaScript-3格式):
var a = tracking()(ft, false);
console.log(a);
// the console.log(a) should provide as a result: ft=gender.radio|F|0;firstName.text|...; false;
我的解决方案(不起作用)
function tracking() {
return function (ft, submitted) {
var arr = [];
for (var i = 0; i < ft.length; i++) {
var elem = ft[i];
arr.push(elem.name + "." + elem.type + "|" + elem.value + "|" +
elem.focus + "submitted");
}
var str = arr.join(";");
}
}
谢谢!
答案 0 :(得分:1)
看来您已经使这一点变得复杂了。可以这样简单
function tracking (ft, submitted) {
var arr = [];
for (var i = 0; i < ft.length; i++) {
var elem = ft[i];
arr.push(elem.name + "." + elem.type + "|" + elem.value + "|" +
elem.focus + "submitted");
}
return arr.join(";");
}
我怀疑您为什么要提交该参数。如果要将其用作变量,只需在将其推入数组
时除去引号