简体   繁体   English

如何将JSON响应映射到下拉列表?

[英]How to map JSON response to a dropdown list?

{customerCode: "730", accountId: 9, dueAmount: 0, connectionId: 116303, accountName: "Connection"}

I have a JSON response like this. 我有这样的JSON响应。 I need to add the account accountName to dropdown label and accountId to its curresponding value field. 我需要将帐户accountName添加到下拉标签,并将accountId添加到其对应的值字段。

accNames = accNames.filter(function(item, index,inputArray) {
        return inputArray.indexOf(item) == index;
        });

I am using the above code to filter the duplicate account names. 我正在使用上面的代码来过滤重复的帐户名。

var sel = document.getElementById('selectAccount');
        for (var i = 0; i < accNames.length; i++) {
            var opt = document.createElement('option');
                opt.innerHTML = accNames[i];
                opt.value = accIds[i];
                sel.appendChild(opt);
        }       

The above code is used to add the accountName and accountId to dropdown. 上面的代码用于将accountName和accountId添加到下拉列表中。 but the accountName doesn't matches the accountId. 但是accountName与accountId不匹配。 There is some problem in mapping accountid to accountname. 将accountid映射到accountname时出现问题。 Anyone please give me some suggestions. 有人请给我一些建议。

If each account name has an associated accountid and it is unique for each accountname you can filter the second array too. 如果每个帐户名称都有一个关联的帐户ID,并且对于每个帐户名称都是唯一的,则您也可以过滤第二个数组。

accIds = accIds.filter(function(item, index,inputArray) {
        return inputArray.indexOf(item) == index;
        });

If an accountname has 2 occurrence its curresponding id also get filtered. 如果一个帐户名有2次出现,则其对应的ID也将被过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM