gpt4 book ai didi

javascript - 如何在javascript/jquery中拼接数组中的值?

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

我已将数组声明为 var user_profile = []; 并且我已经在 arrayList 中推送了一些数据,其中包含:

[  
{
"assessmentType":"PRE",
"assessCatId":0,
"assessReason":"appeal",
"assessAmount":"445",
"assessPenalty":"",
"assessTotal":445
},
{
"assessmentType":"PRE",
"assessCatId":0,
"assessReason":"appeal",
"assessTotal":null
},
{
"assessmentType":"PRE",
"assessCatId":0,
"assessReason":"445",
"assessTotal":null
},
{
"assessmentType":"PRE",
"assessCatId":0,
"assessReason":"",
"assessTotal":null
},
{
"assessmentType":"PRE",
"assessCatId":0,
"assessReason":"2075-09-13",
"assessAmount":"2075-09-03",
"assessTotal":null
},
{
"assessmentType":"PRE",
"assessCatId":0,
"assessReason":"2075-09-13",
"assessTotal":null
},
{
"assessmentType":"PRE",
"assessCatId":0,
"assessReason":"2075-09-03",
"assessTotal":null
},
{
"assessmentType":"PRE",
"assessCatId":1,
"assessReason":"A",
"assessAmount":"1",
"assessPenalty":"2",
"assessTotal":3
},
{
"assessmentType":"PRE",
"assessCatId":2,
"assessReason":"B",
"assessAmount":"3",
"assessPenalty":"40",
"assessTotal":43
},
{
"assessmentType":"PRE",
"assessCatId":3,
"assessReason":"C",
"assessAmount":"5",
"assessPenalty":"6",
"assessTotal":11
}
]

在这些数组列表中,我需要删除 assessCatId==0 的数据。所以我尝试使用

进行拼接
$.each(user_profile, function(i) {
if (user_profile[i].assessCatId == 0) {
user_profile.splice(i, 1);
}
});

但它不起作用。它向我显示错误:

enter image description here

或者我的意思是说我只需要 arrayList 中 assessCatId==1 或 2 或 3 的数据。 如何过滤此 arrayList 并从 arrayList 中删除这些数据?

最佳答案

在迭代数组时,不要尝试改变(例如拼接)数组 - 这会导致非常不直观的行为。例如,在第一个元素被删除后,进一步迭代的 i 将引用正在迭代的元素使用所在的索引,而不是该元素所在的索引at 在新的变异数组中 - 导致接近尾部时出现未定义问题,并且删除了不正确的索引。

一个简单的 .filter 在这里将是一个更好的选择,不需要 jQuery:

const input=[{"assessmentType":"PRE","assessCatId":0,"assessReason":"appeal","assessAmount":"445","assessPenalty":"","assessTotal":445},{"assessmentType":"PRE","assessCatId":0,"assessReason":"appeal","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"445","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"2075-09-13","assessAmount":"2075-09-03","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"2075-09-13","assessTotal":null},{"assessmentType":"PRE","assessCatId":0,"assessReason":"2075-09-03","assessTotal":null},{"assessmentType":"PRE","assessCatId":1,"assessReason":"A","assessAmount":"1","assessPenalty":"2","assessTotal":3},{"assessmentType":"PRE","assessCatId":2,"assessReason":"B","assessAmount":"3","assessPenalty":"40","assessTotal":43},{"assessmentType":"PRE","assessCatId":3,"assessReason":"C","assessAmount":"5","assessPenalty":"6","assessTotal":11}]

const output = input.filter(({ assessCatId }) => assessCatId != 0);
console.log(output);

关于javascript - 如何在javascript/jquery中拼接数组中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53940028/

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