Javascript 使用对象中的值用嵌套名称填充输入

Javascript 使用对象中的值用嵌套名称填充输入,javascript,jquery,html,json,nested,Javascript,Jquery,Html,Json,Nested,我有这样的输入: <input type="text" id="contact[actif]" name="contact[actif]"> <input type="text" id="contact[sql]" name="contact[sql]"> <input type="text" id="interlocuteur[actif]" name="interlocuteur[actif]"> <input type="text" id="int

我有这样的输入:

<input type="text" id="contact[actif]" name="contact[actif]">
<input type="text" id="contact[sql]" name="contact[sql]">
<input type="text" id="interlocuteur[actif]" name="interlocuteur[actif]">
<input type="text" id="interlocuteur[sql]f" name="interlocuteur[sql]">

如何用对象的值填充输入?

假设问题的关键是,您在运行时不知道对象键的名称,您可以使用两个循环来遍历从JSON反序列化的对象键。然后可以使用检索到的键为元素构建选择器字符串,如下所示:

风险值数据={ 联系人:{ 是的, sql:从w中选择* }, 对话者:{ 是的, sql:从l中选择* } } Object.keysdata.forEachfunctionouterKey{ Object.keysdata[outerKey].forEachfunctioninnerKey{ 变量选择器=`${outerKey}\\\[${innerKey}\\\\]`; $selector.valdata[outerKey][innerKey]; }; }; 使用JavaScript:

document.getElementById("contact[actif]").value = contact.actif;
document.getElementById("contact[sql]").value = contact.sql;

document.getElementById("interlocuteur[actif]").value = interlocuteur.actif;
document.getElementById("interlocuteur[sql]").value = interlocuteur.sql;

我如何才能使foreach在与我的JSON相同的顺序上工作?你不能,因为对象中属性的顺序是不保证的。如果需要,则需要使用数组而不是对象。
document.getElementById("contact[actif]").value = contact.actif;
document.getElementById("contact[sql]").value = contact.sql;

document.getElementById("interlocuteur[actif]").value = interlocuteur.actif;
document.getElementById("interlocuteur[sql]").value = interlocuteur.sql;