我有一些代码
function getSomeInfoFromDB() {
let promises = [];
let order = [];
$("tr").each(function(index) {
let value1 = $(this).children("td:nth-child(4)").text();
let value2 = $(this).children("td:nth-child(5)").text();
order.push({
index: index,
a: value1,
b: value2,
promiseResult: ""
});
promises.push(new Promise((resolve) =>
$.post("http://url.com/getDataFromDataBase.php", {a: value2})
.done(resolve)));
});
Promise.all(promises)
.then(values => values)
.then(result => $(order).each(function() {
this.promiseResult = result;
}))
.then(console.log(order));
}
我需要保存索引,value1,value2和PromiseResult(每次迭代这个$的DataBase答案) 将每个TR元素转换为关联数组或其他东西 我无法得到正确的结果。
我需要类似这个数组的内容 - order[{index: 1, a: value1forIndex1 , b: value2forIndex1, promiseResult: REZULT1} ,{index: 2, a: value1forIndex2 , b: value2forIndex2, promiseResult: REZULT2}]
答案 0 :(得分:1)
这应该适用于您要求的内容
function getSomeInfoFromDB() {
var promises = $('tr').map((index, elem) => {
let val1 = $(elem).children("td:nth-child(4)").text();
let val2 = $(elem).children("td:nth-child(5)").text();
return $.post('http://url.com/getDataFromDataBase.php', { a: val2 }).then(result => {
return {
index: index,
a: val1,
b: val2,
result: result
};
});
});
Promise.all(promises)
.then(arr => {
console.log(arr);
});
}