arrays - 返回相同元素的数组过滤器
问题描述
我正在尝试在数组中上下移动问题,因此我更新了对象中的序列号并根据它对它们进行排序。但是即使数组包含所需的值,以下过滤器语句也会继续返回相同的元素。
像这样修改对象会在原始数组中更新它们吗?
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
解决方案
推荐阅读
- javascript - 悬停下拉导航在边距、javascript 和 css 上移动时消失
- visual-studio-code - 如何处理 VS Code 中多个问题匹配器报告的重复问题?
- animation - 无法在 Blender 中渲染对象
- css - “宽度:100%;最大宽度:100%;” 设置这两个是否有特殊原因?
- php - Socket.io 3 和 PHP 集成
- c# - 设备进入睡眠状态后保持 Unity 程序在后台运行?
- ios - 获取大标题导航栏默认收缩和扩展高度
- node.js - 为使用 `require` 导入的对象分配别名
- assembly - 汇编,将单个字节从寄存器写入内存会覆盖其他字节
- python - 无法从网页中抓取所有公司名称