gpt4 book ai didi

javascript - 在 Javascript 中循环对象数组

转载 作者:行者123 更新时间:2023-12-03 04:21:14 26 4
gpt4 key购买 nike

我试图理解这一点,但由于某种原因我无法理解。我对 Javascript 还很陌生......

这就是我想做的。

var companyList = {};
companyList.comanyZero = {
fldStreet: "That street 0",
fldPostcode: "0000 AA",
fldCity: "Amsterdam",
fldCountry: "The Netherlands"
};
companyList.companyOne = {
fldStreet: "Street name 1",
fldPostcode: "1234 BA",
fldCity: "Amsterdam",
fldCountry: "The Netherlands"
};

if (!event.willCommit){

if (event.changeEx === "companyZero"){
this.getField("fldStreet").value = companyList.companyZero.fldStraat;
this.getField("fldPostcode").value = companyList.companyZero.fldPostcode;
this.getField("fldCity").value = companyList.companyZero.fldCity;
this.getField("fldCountry").value = companyList.companyZero.fldCountry;
}

if (event.changeEx === "companyOne"){
this.getField("fldStreet").value = companyList.companyOne.fldStreet;
this.getField("fldPostcode").value = companyList.companyOne.fldPostcode;
this.getField("fldCity").value = companyList.companyOne.fldCity;
this.getField("fldCountry").value = companyList.companyOne.fldCountry;
}
}

这个想法是创建一个循环,根据组合框中选择的内容返回值。

请注意,这是 PDF 审核,因此用户可以更改 PDF 中的组合框,并且字段将填充正确的数据。

最佳答案

我认为您只是在寻找括号符号:

if (!event.willCommit) {
var currentCo = companyList[event.changeEx];
if (currentCo) {
this.getField("fldStreet").value = currentCo.fldStraat;
this.getField("fldPostcode").value = currentCo.fldPostcode;
this.getField("fldCity").value = currentCo.fldCity;
this.getField("fldCountry").value = currentCo.fldCountry;
}
}

如果 event.changeEx 包含 "companyOne",则 companyList[event.changeEx] 将引用 companyList.companyOne.

<小时/>

由于您的属性名称和字段名称相同,您甚至可以维护要获取的键列表并使用循环更新其值:

var fieldNames = ["fldStreet", "fldPostcode", "fldCity", "fldCountry"]
if (!event.willCommit) {
var currentCo = companyList[event.changeEx];
if (currentCo) {
fieldNames.forEach((name) => {
this.getField("").value = currentCo[name];
})
}
}

关于javascript - 在 Javascript 中循环对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43937269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com