3 回答

TA貢獻1873條經(jīng)驗 獲得超9個贊
您可以像這樣將列表獲取到頁面。然后您可以在每個循環(huán)中按 div 或 ul 列表內(nèi)部。
function findEmployees(userCounty) {
$.ajax({
type: "POST",
dataType: "json",
url: '@Url.Action("getCounty", "Contact")',
data: JSON.stringify(userCounty),
contentType: "application/json",
success: function (result) {
if (result.data.length !== 0) {
$.each(result.data, function (index, value) {
var firstName = value.firstName;
var lastName = value.lastName;
});
}
},
});
}

TA貢獻1790條經(jīng)驗 獲得超9個贊
您可以使用reduce和findIndex
const list = [
["Hampton Tricep Rope", 3],
["Chrome Curl Bar", 8],
["Hampton Tricep Rope", 6]
].reduce((acc, x) => {
const index = acc.findIndex(y => y[0] === x[0]);
if (index >= 0) {
acc[index][1] += x[1];
return acc;
}
acc.push(x);
return acc;
}, [])
console.log(list)

TA貢獻1802條經(jīng)驗 獲得超4個贊
var result = [];
[["Hampton Tricep Rope", 3],["Chrome Curl Bar",8],["Hampton Tricep Rope", 6]].reduce(function(res, value) {
if (!result.filter((item) => (item[0] === value[0])).length) {
result.push([value[0], value[1]]);
} else {
result.filter((item) => (item[0] === value[0]))[0][1] += value[1];
}
return res;
}, {});
我們需要構建一個數(shù)組,同時減少另一個數(shù)組,如上所示。在每一步中,我們都會檢查是否已經(jīng)擁有該元素。如果沒有,那么我們添加它。否則我們根據(jù)需要增加它。
添加回答
舉報