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

时间:2017-05-26 11:23:48

标签: javascript jquery arrays json object

我有一个按钮,应该“编译”一个将在终端中使用的命令 保持解释简单:
行内有六个输入,行是一个对象,内部的输入是属性,输入值是属性的值。 从输入数据写入终端命令。

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

修改 非常感谢您的快速解答!现在感谢你们,我希望现在变得更聪明了: - )

4 个答案:

答案 0 :(得分:3)

因为你需要通过keyname获取它,就像那样(快速示例);

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:3)

你混淆了数组和对象。

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);

 

答案 2 :(得分: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 = "";
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);

答案 3 :(得分:2)

循环中的i正在计算密钥,formsData中有2个密钥。因此i取值0和1.当您执行formsData[i]时,您实际上正在执行formsData[0]formsData[1],这两个都不存在。这就是你未定义的原因。

您可以尝试formsData[keys[i]]。这种方式keys[i]正在采用值&#34; transfer_1&#34;和&#34; transfer_2&#34;,所以formsData["transfer_1"].host_1突然变得有效。