开发者问题收集

从嵌套对象的属性生成代码字符串

2017-05-26
53

我有一个按钮,可以“编译”将在终端中使用的命令。
解释简单一点:
行内有六个输入,行是一个对象,里面的输入是属性,输入的值是属性的值。 根据输入数据编写终端命令。

JSON 对象如下所示

formsData = {
  transfer_1: {
    host_1: "t1_host1",
    host_2: "t1_host2",
    user_1: "t1_user1",
    user_2: "t1_user2",
    password_1: "t1_password1",
    password_2: "t1_password2"
  },
  transfer_2: {
    host_1: "t2_host1",
    host_2: "t2_host2",
    user_1: "t2_user1",
    user_2: "t2_user2",
    password_1: "t2_password1",
    password_2: "t2_password2"
  }
}

我想将 transfer_1 中的所有值放入终端的命令中,如下所示:

./imapsync --host1 t1_host1 --user1 t1_user1 --password1 t1_password1 
--host2 t1_host2 --user2 t1_user2 --password2 t1_password2;

我想做这样的事情:

var command = "";
for (i = 0; i < Object.keys(formsData).length; i++) {
    command += 
    "./imapsync_bin_Darwin " +
    "--host1 " + formsData[i].host_1 + " " +
    "--user1 " + formsData[i].user_1 + " " +
    "--password1 " + formsData[i].password_1 + " " +
    "--host2 " + formsData[i].host_2 + " " +
    "--user2 " + formsData[i].user_2 + " " +
    "--password2 " + formsData[i].password_2 + "; ";
}
console.log(command);

但我得到的只是

Uncaught TypeError: Cannot read property 'host_1' of undefined

编辑: 非常感谢您快速回答!感谢你们,现在它已经起作用了,我希望现在我变得更聪明了 :-)

3个回答

因为你需要通过键名来获取它,就像这样(简单示例);

var formsData = {
  transfer_1: {
    host_1: "t1_host1",
    host_2: "t1_host2",
    user_1: "t1_user1",
    user_2: "t1_user2",
    password_1: "t1_password1",
    password_2: "t1_password2"
  },
  transfer_2: {
    host_1: "t2_host1",
    host_2: "t2_host2",
    user_1: "t2_user1",
    user_2: "t2_user2",
    password_1: "t2_password1",
    password_2: "t2_password2"
  }
};

var command = "",
    keys = Object.keys(formsData);

for (i = 0; i < keys.length; i++) {
    command += 
    "./imapsync_bin_Darwin " +
    "--host1 " + formsData[keys[i]].host_1 + " " +
    "--user1 " + formsData[keys[i]].user_1 + " " +
    "--password1 " + formsData[keys[i]].password_1 + " " +
    "--host2 " + formsData[keys[i]].host_2 + " " +
    "--user2 " + formsData[keys[i]].user_2 + " " +
    "--password2 " + formsData[keys[i]].password_2 + "; ";
}
console.log(command);
DanilGholtsman
2017-05-26

试试这个,

var formsData = {
  transfer_1: {
    host_1: "t1_host1",
    host_2: "t1_host2",
    user_1: "t1_user1",
    user_2: "t1_user2",
    password_1: "t1_password1",
    password_2: "t1_password2"
  },
  transfer_2: {
    host_1: "t2_host1",
    host_2: "t2_host2",
    user_1: "t2_user1",
    user_2: "t2_user2",
    password_1: "t2_password1",
    password_2: "t2_password2"
  }
};
var command = "";
var keys=Object.keys(formsData);
for (var i=0,l=keys.length;i<l;i++) {
    t=formsData[keys[i]];
    command += 
    "./imapsync_bin_Darwin " +
    "--host1 " + t.host_1 + " " +
    "--user1 " + t.user_1 + " " +
    "--password1 " + t.password_1 + " " +
    "--host2 " + t.host_2 + " " +
    "--user2 " + t.user_2 + " " +
    "--password2 " + t.password_2 + "; ";
}
console.log(command);
Rohan Kumar
2017-05-26

您混淆了数组和对象。

formsData 是一个对象,但您将其用作数组(尝试获取 length 并使用索引对其进行迭代)。使用 for (var key in formsData) 并在之后使用 hasOwnProperty 检查对象。

像这样(未经测试,但您明白了):

var command = "";
for (var key in formsData) {
    if (!formsData.hasOwnProperty(key)) continue;
    command += 
    "./imapsync_bin_Darwin " +
    "--host1 " + formsData[key].host_1 + " " +
    "--user1 " + formsData[key].user_1 + " " +
    "--password1 " + formsData[key].password_1 + " " +
    "--host2 " + formsData[key].host_2 + " " +
    "--user2 " + formsData[key].user_2 + " " +
    "--password2 " + formsData[key].password_2 + "; ";
}
console.log(command);

您可能希望在第一个键(“transfer_1”)之后中断,以便获得您真正想要的内容(“将 transfer_1 中的所有值放入命令中”)。因此,您实际上可以不使用循环,只需分配 var key = "transfer_1"

对于更短的方法,您可以像这样更漂亮地完成它:

var formsData = {
  transfer_1: {
    host_1: "t1_host1",
    host_2: "t1_host2",
    user_1: "t1_user1",
    user_2: "t1_user2",
    password_1: "t1_password1",
    password_2: "t1_password2"
  },
  transfer_2: {
    host_1: "t2_host1",
    host_2: "t2_host2",
    user_1: "t2_user1",
    user_2: "t2_user2",
    password_1: "t2_password1",
    password_2: "t2_password2"
  }
}

var command = "./imapsync_bin_Darwin ";
for (var key in formsData.transfer_1) {
    if (!formsData.transfer_1.hasOwnProperty(key)) continue;
    command += "--" + key + " " + formsData.transfer_1[key] + " ";
}
command = command.slice(0,-1) + ";";
console.log(command);

 
Franz B.
2017-05-26