gpt4 book ai didi

javascript - 如何使用递归迭代包含数组、对象、k-v 对的 Json 响应?

转载 作者:行者123 更新时间:2023-12-01 01:21:06 25 4
gpt4 key购买 nike

我有一个 json 响应,目前我正在使用 for 循环进行迭代。我想消除 for 循环并使用递归函数来缩短它。$searchModal 是模态代码,我在其中迭代 k-v 对并附加到模态。

>Json response :
>ActiveFlag: -1
>Address: [{…}]
>BadAddressDate: null
>ContactID: 9999
>ContactName: "William Clem Dec"
>DateOfBirth: "1972-08-17T00:00:00"
>Phone: [{…}]
>PrimaryFirstName: "William"
>PrimaryLastName: "Clem Dec"
>PrimaryMiddleInitial: null
>RoleCode: 0
>RoleCodeDescription: null
>TaxID: "338722304"
>TaxIDType: "SSN"
>Udfs: {}


var contactMap = {
"url": `${contactUrl}`,
"id": 'contactData',
"node": '#contactData',
"label": "Contact",
"message": "Contact Not Found",
"tab": "contact"
};

function search(map) {
return new Promise(function (resolve) {
http.get(map.url).then(function (data) {
if (data.length != 0) {
var val = 1;
$searchModal.find('#tabContent').append(`<li><a href="#${map.tab}" aria-controls="home" role="tab"
data-toggle="tab">${map.label}</a></li>`);
for (var i = 0; i < data.length; i++) {
$searchModal.find('#data').append(`<div role="tabpanel" class="tab-pane fade" id=${map.tab}><div class="panel-body" id=${map.id}><div class="col-xs-12"><label> ${map.label} ${val}</label></div>`);
var val = val + 1;
for (key in data[i]) {
var index = 1;
if (key == "Udfs") {
$searchModal.find(map.node).append(`<div class="col-xs-12"><label> ${key} </label></div>`);
for (udfKey in data[i][key]) {
$searchModal.find(map.node).append(`<div class="col-lg-3" ><div class="input-group"> <span class="input-group-addon" id="key">${udfKey}</span> <input id="accountModelId" type="text" disabled class="form-control" placeholder="" aria-describedby="accountModel" value="${data[i][key][udfKey]}"></div></div>`);
}
}
for (var j = 0;
(Array.isArray(data[i][key])) && j < data[i][key].length; j++) {
$searchModal.find(map.node).append(`<div class="col-xs-12"><label> ${key} ${index}</label></div>`);
var index = index + 1;
for (objKey in data[i][key][j]) {
$searchModal.find(map.node).append(`<div class="col-lg-3" ><div class="input-group"> <span class="input-group-addon" id="key">${objKey}</span> <input id="accountModelId" type="text" disabled class="form-control" placeholder="" aria-describedby="accountModel" value="${data[i][key][j][objKey]}"></div></div>`);
}
}
if (!Array.isArray(data[i][key]) && !(data[i][key] instanceof Object)) {
$searchModal.find(map.node).append(`<div class="col-lg-3" ><div class="input-group"> <span class="input-group-addon" id="key">${key}</span> <input id="accountModelId" type="text" disabled class="form-control" placeholder="" aria-describedby="accountModel" value="${data[i][key]}"></div></div>`);
}
}
$searchModal.find('#data').append(`</div>`);
$searchModal.find('#data').append(`</div>`);
}
}
resolve(console.log(data));
if (data.length == 0) {
alerts.error(map.message)
}
}).catch(function err() {
$searchModal.show("modal")
alerts.error(map.message)
spinners.stop(spinners.start(spinners.WANDERING_CUBES, $searchModal))
});
});

}

响应看起来像 this .

有什么好的建议来重构代码以避免 for 循环吗?

最佳答案

要迭代对象,您可以使用:

let record = {'key1': 'value1', 'key2': {'test':'123'}, 'key3': [{'test1':'23'}]};

if(typeof record === 'object' && record != null){
Object.keys(record).forEach((key)=>{
if(typeof record[key] == 'object' && record[key] != null){
//logic for the object
}else if(typeof record[key] == 'array' && record[key] != null){
//logic for array
}else {
//logic for other type
}
});
}else if(typeof record === 'array'){
record.forEach(r => {
if(typeof r == 'object' && r != null){
//logic for the object
}else if(typeof r == 'array' && r != null){
//logic for array
}else {
//logic for other type
}
});
}

关于javascript - 如何使用递归迭代包含数组、对象、k-v 对的 Json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226623/

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