3 回答
TA贡献1820条经验 获得超10个赞
使用 for in 迭代您的对象,如下所示。
for(var item in data){
if (data[item]["tomany"] == true && data[item]["fileName"] !=""){
// perform your task here
}
}
TA贡献1884条经验 获得超4个赞
如果满足条件,与其遍历数据并执行 AJAX 调用,不如拆分代码以便一次完成一项工作。
1) 迭代对象并根据条件生成一组 AJAX 承诺。
2) 使用 jQuery$.when等待所有调用完成,然后
3)根据结果更新文件列表
const data = {"record-164": {"tomany": true, "fileName": "164"}, "record-165": {"tomany": true, "fileName": "165"}};
function doFetch(filename) {
return $.ajax({ ... });
}
function getPromises(data) {
return Object.keys(data).reduce((acc, obj) => {
const { tomany, fileName } = obj;
if (tomany && filename){
const promise = doFetch(filename);
return acc.concat(promise);
}
return acc;
}, []);
}
$.when.apply(null, getPromises()).then(result => {
updateFilenames(result);
});
TA贡献1775条经验 获得超11个赞
首先,它是一个对象,而不是数组。
您可以使用此区分对象和数组
以{->开头object
例子
const d = {'name':'your-name', 'age':23};
以[->开头array
const d_array = [{'name':'your-name', 'age':23},{'name':'your-name-2', 'age':26}];
您可以像这样迭代您的对象,然后执行 ajax。
const data = {"record-164": {"tomany": true, "fileName": "164"},"record-165": {"tomany": true, "fileName": "165"}}
for(let key in data){
console.log(data[key]["tomany"]);
console.log(data[key]["fileName"]);
}
添加回答
举报
