首页 > 解决方案 > 返回相同元素的数组过滤器

问题描述

我正在尝试在数组中上下移动问题,因此我更新了对象中的序列号并根据它对它们进行排序。但是即使数组包含所需的值,以下过滤器语句也会继续返回相同的元素。

像这样修改对象会在原始数组中更新它们吗?

var questiontoswap = this.questions.filter(x => x.sequence == question.sequence + 1 && ("state" in x) ? (x.state != 3) : true)[0];
var originalquestion = this.questions.filter(x => x.sequence == question.sequence && ("state" in x) ? (x.state != 3) : true
                            )[0];
questiontoswap.sequence -= 1;
originalquestion.sequence += 1;
let filteredquestions =this.questions.filter(x => x.state != 3).sort((a, b) => (a.sequence < b.sequence ? -1 : 1));
           

大批

questions: (3) […]
​​​
0: {…}
​​​​
questionId: 7
​​​​
questionLanguages: (4) […]
​​​​
questionType: "Short answer"
​​​​
questionTypeId: 2
​​​​
rowId: 1
​​​​
sequence: 1
​​​​
<prototype>: {…}
​​​
1: {…}
​​​​
questionId: 13
​​​​
questionLanguages: (4) […]
​​​​
questionType: "MCQ"
​​​​
questionTypeId: 3
​​​​
rowId: 2
​​​​
sequence: 2
​​​​
<prototype>: {…}
​​​
2: {…}
​​​​
questionId: 19
​​​​
questionLanguages: (4) […]
​​​​
questionType: "Likert"
​​​​
questionTypeId: 4
​​​​
rowId: 3
​​​​
sequence: 3
​​​​
<prototype>: {…}
​​​
length: 3

标签: arraystypescript

解决方案


推荐阅读