Javascript 如何根据参数对对象数组进行排序
我有一个类似json的对象数组作为响应:Javascript 如何根据参数对对象数组进行排序,javascript,arrays,Javascript,Arrays,我有一个类似json的对象数组作为响应: const arr = [{ Mandatory: false, Question: 65, QuestionType: "E", YesNo: "n", type: "QuestionOutput" }, { Mandatory: false, Question: 64, QuestionType: "H", YesNo: "n", type: "QuestionO
const arr = [{
Mandatory: false,
Question: 65,
QuestionType: "E",
YesNo: "n",
type: "QuestionOutput"
},
{
Mandatory: false,
Question: 64,
QuestionType: "H",
YesNo: "n",
type: "QuestionOutput"
},
{
Mandatory: false,
Question: 85,
QuestionType: "S",
YesNo: "n",
type: "QuestionOutput"
},
{
Mandatory: false,
Question: 61,
QuestionType: "F",
YesNo: "",
type: "QuestionOutput"
},
{
Mandatory: true,
Question: 60,
QuestionType: "F",
YesNo: "",
type: "QuestionOutput"
}
];
我想根据属性问题对这个数组进行升序排序,并将排序后的对象数组放入另一个对象数组中 您可以使用语法arr.sort(compareFunction)
,注意,由于对象中的属性值是一个整数
,您可以排序:
- ASC:
arr.sort((a,b)=>a.Question-b.Question)
- DESC:
arr.sort((a,b)=>b.Question-a.Question)
const arr=[{强制:假,问题:65,问题类型:“E”,YesNo:“n”,类型:“问题输出”},{强制:假,问题:64,问题类型:“H”,YesNo:“n”,类型:“问题输出”},{强制:假,问题:85,问题类型:“S”,YesNo:“n”,类型:“问题输出”},{强制:假,问题:61,问题类型:“F”,YesNo:,类型:“QuestionOutput”},{必填:true,问题:60,问题类型:“F”,YesNo:,类型:“QuestionOutput”}];
arr.sort((a,b)=>a.问题-b.问题);
console.log('arr:',arr);
.as console wrapper{max height:100%!important;top:0;}
您的意思是按字母顺序排序吗?您希望按什么逻辑排序?请不要将其格式化,因为这里的对象属性值是一个整数
@YosvelQuintero:还有很多其他重复项。