json如何排序?

代码如下:

const arr = [{

align: "center",

fixed: false,

label: "测试项目",

prop: "factorName",

width: 35

}, {

align: "center",

fixed: false,

label: "测试编号",

prop: "sampleName",

width: 35

}, {

align: "center",

fixed: false,

label: "最终结果",

prop: "resultValue",

width: 35

}, {

align: "center",

fixed: false,

label: "判定标准编号",

prop: "standardNo",

width: 35

}, {

align: "center",

fixed: false,

label: "判定标准名称",

prop: "standardName",

width: 35

}, {

align: "center",

fixed: false,

label: "标准值下限",

prop: "standardLowerLimit",

width: 35

}, {

align: "center",

fixed: false,

label: "标准值上限",

prop: "standardLimit",

width: 35

}]

在页面上展示测试项目、测试编号、最终结果、标准值上限、标准值下限、判定标准编号、判定标准名称
大佬们这种要怎么排序呢

    const columns = res.result.checked.filter(o => {

return o.key !== "standardChildName" && o.key !== "qualityType"

})

this.columns = columns.map(item => {

let obj = {

prop: item.key,

label: item.title,

align: 'center',

width:['standardName','standardNo'].indexOf(item.key) > -1 ? 100 : 35,

fixed: false

}

return obj

})


回答:

定一下顺序

const sortOrder = ["factorName", "sampleName", "resultValue", "standardLimit", "standardLowerLimit", "standardNo", "standardName"];

然后排序:

const sortedArr = sortOrder.map(prop => arr.find(item => item.prop === prop));

const columns = res.result.checked.filter(o => {

return o.key !== "standardChildName" && o.key !== "qualityType";

}).map(item => {

let obj = {

prop: item.key,

label: item.title,

align: 'center',

width: ['standardName', 'standardNo'].indexOf(item.key) > -1 ? 100 : 35,

fixed: false

};

return obj;

});

const sortOrder = ['factorName', 'sampleName', 'resultValue', 'standardLimit', 'standardLowerLimit', 'standardNo', 'standardName'];

const sortedColumns = sortOrder.map(prop => {

return columns.find(item => item.prop === prop);

});

const remainingItems = columns.filter(item => !sortOrder.includes(item.prop));

const finalSortedColumns = sortedColumns.concat(remainingItems);

this.columns = finalSortedColumns;


回答:

<el-table

:data="arr"

stripe

style="width: 100%">

<el-table-column

prop="date"

label="测试项目"

width="180">

</el-table-column>

<el-table-column

prop="name"

label="测试编号"

width="180">

</el-table-column>

<el-table-column

prop="address"

label="最终结果">

</el-table-column>

<el-table-column

prop="address"

label="标准值上限">

</el-table-column><el-table-column

prop="address"

label="标准值下限">

</el-table-column>

直接这样就行了


回答:

JSON数据转成JS的数组被,然后按照你需求的顺序排序就行了,比如说你问题中描述的顺序:

测试项目、测试编号、最终结果、标准值上限、标准值下限、判定标准编号、判定标准名称

那么写一个 sort 方法就行了。

js">// JSON 数据

var arr = [JSON_DATA]

// 规定的数据顺序

var sortList = ['测试项目', '测试编号', '最终结果', '标准值上限', '标准值下限', '判定标准编号', '判定标准名称']

// 数据排序

var newArr = arr.sort((a,b) => sortList.indexOf(a.label) - sortList.indexOf(b.label))

用到的两个API:
Array.prototype.sort()
Array.prototype.indexOf()

以上是 json如何排序? 的全部内容, 来源链接: utcz.com/p/934784.html

回到顶部