sort
排序后会改变原有数组。 简单数组和对象数组都可以进行排序。 默认升序排序。
< template>
< div class = " myBlock" >
< div class = " tableBlock" >
< div class = " title" >
{{ newObject ? "操作后的数组" : "操作前的数组" }}
</ div>
< el-table :data = " tableData" :key = " num" stripe border >
< el-table-column prop = " index" label = " 序号" width = " 200" >
</ el-table-column>
< el-table-column prop = " name" label = " 昵称" > </ el-table-column>
</ el-table>
< div class = " simpleTable" >
< div class = " title" > 提取序号简单数组</ div>
< div class = " indexArr" >
< div v-for = " (item, index) in indexArr" :key = " index" >
{{ item }}
</ div>
</ div>
< div class = " title" > 提取昵称简单数组</ div>
< div class = " nameArr" >
< div v-for = " (item, index) in nameArr" :key = " index" >
{{ item }}
</ div>
</ div>
</ div>
</ div>
< div class = " operate" >
< el-button type = " success" plain @click = " operate(1)" >
sort-提取序号-默认排序
</ el-button>
< el-button type = " success" plain @click = " operate(2)" >
sort-提取序号-控制升降
</ el-button>
< el-button type = " success" plain @click = " operate(3)" >
sort-提取昵称-默认排序
</ el-button>
< el-button type = " success" plain @click = " operate(4)" >
sort-使用序号-控制升降
</ el-button>
</ div>
< div class = " title" v-if = " newObject" > 操作后的返回值</ div>
< div class = " newObject" >
{{ newObject }}
</ div>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
tableData : [
{ index : "20" , name : "ac" } ,
{ index : "11" , name : "bz" } ,
{ index : "25" , name : "az" } ,
{ index : "4" , name : "zz" } ,
] ,
indexArr : [ ] ,
nameArr : [ ] ,
num : 0 ,
newObject : null ,
} ;
} ,
watch : { } ,
mounted ( ) {
this . indexArr = [ ] ;
this . nameArr = [ ] ;
this . tableData. forEach ( ( item, index ) => {
this . indexArr. push ( item. index) ;
} ) ;
this . tableData. forEach ( ( item, index ) => {
this . nameArr. push ( item. name) ;
} ) ;
} ,
methods : {
operate ( flag ) {
if ( flag == 1 ) {
this . newObject = this . indexArr. sort ( ) ;
} else if ( flag == 2 ) {
this . newObject = this . indexArr. sort ( function ( prev, next ) {
return prev - next;
} ) ;
} else if ( flag == 3 ) {
this . newObject = this . nameArr. sort ( ) ;
} else if ( flag == 4 ) {
this . newObject = this . tableData. sort ( function ( prev, next ) {
return prev. index - next. index;
} ) ;
}
this . num += 1 ;
} ,
} ,
} ;
</ script>
操作前
操作后
sort-提取序号-默认排序
this . newObject = this . indexArr. sort ( ) ;
先对比数字的第一位,再对比第二位。 与 number 类型的升序逻辑不同。 即使是 number 类型的数值参与排序,也会被转换成字符串类型排序。
sort-提取序号-控制升降
this . newObject = this . indexArr. sort ( function ( prev, next ) {
return prev - next;
} ) ;
复杂排序时,控制升降,用到了运算符,所以将字符串类型的内容转换成了 number 类型进行排序 根据 return 的值可以控制是升序还是降序
sort-提取昵称-默认排序
this . newObject = this . nameArr. sort ( ) ;
先比较第一位字母,再比较第二位字母 按照码表进行排序
sort-使用序号-控制升降
this . newObject = this . tableData. sort ( function ( prev, next ) {
return prev. index - next. index;
} ) ;
原始的数组在排序后会被改变。 返回值为改变顺序后的数组。
join
使用字符将数组内容拼接起来。 返回值为字符串类型。 不会改变拼接前数组的内容。 可用于拼接键值对数组内容(如下示例)。
< template>
< div class = " myBlock" >
< div class = " tableBlock" >
< div class = " title" > 原始数组</ div>
< el-table :data = " tableData" :key = " num" stripe border >
< el-table-column prop = " key" label = " 键名" > </ el-table-column>
< el-table-column prop = " value" label = " 值" > </ el-table-column>
</ el-table>
</ div>
< div class = " operate" >
< el-button type = " success" plain @click = " operate" > join </ el-button>
</ div>
< div class = " title" v-if = " newObject" > 操作后的返回值</ div>
< div class = " newObject" >
{{ newObject }}
</ div>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
tableData : [
{ key : "name" , value : "张三" } ,
{ key : "age" , value : "18" } ,
{ key : "phone" , value : "153****0000" } ,
{ key : "idCard" , value : "100000********0000" } ,
] ,
num : 0 ,
newObject : null ,
} ;
} ,
watch : { } ,
mounted ( ) { } ,
methods : {
operate ( ) {
let keyVal = [ ] ;
this . tableData. forEach ( ( item, index ) => {
keyVal. push ( item. key + "=" + item. value) ;
} ) ;
this . newObject = keyVal. join ( "&" ) ;
} ,
} ,
} ;
</ script>
操作前
操作后
reverse
改变原有数组的排序(反向)。 适用普通数组和对象数组。 原始数组会改变。 返回值为改变顺序后的数组。
< template>
< div class = " myBlock" >
< div class = " tableBlock" >
< div class = " title" > 原始数组</ div>
< el-table :data = " tableData" :key = " num" stripe border >
< el-table-column prop = " key" label = " 键名" > </ el-table-column>
< el-table-column prop = " value" label = " 值" > </ el-table-column>
</ el-table>
</ div>
< div class = " operate" >
< el-button type = " success" plain @click = " operate" >
reverse
</ el-button>
</ div>
< div class = " title" v-if = " newObject" > 操作后的返回值</ div>
< div class = " newObject" >
{{ newObject }}
</ div>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
tableData : [
{ key : "name" , value : "张三" } ,
{ key : "age" , value : "18" } ,
{ key : "phone" , value : "153****0000" } ,
{ key : "idCard" , value : "100000********0000" } ,
] ,
num : 0 ,
newObject : null ,
} ;
} ,
watch : { } ,
mounted ( ) { } ,
methods : {
operate ( ) {
this . newObject = this . tableData. reverse ( ) ;
this . num += 1 ;
} ,
} ,
} ;
</ script>
操作前
操作后
concat
将原始数组与新数组进行拼接。 适用普通数组和对象数组。 不改变原有的数组。 返回值为拼接后的数组。
< template>
< div class = " myBlock" >
< div class = " tableBlock" >
< div class = " title" > 原始数组</ div>
< el-table :data = " tableData" :key = " num" stripe border >
< el-table-column prop = " key" label = " 键名" > </ el-table-column>
< el-table-column prop = " value" label = " 值" > </ el-table-column>
</ el-table>
</ div>
< div class = " operate" >
< el-button type = " success" plain @click = " operate" >
concat
</ el-button>
</ div>
< div class = " title" v-if = " newObject" > 操作后的返回值</ div>
< div class = " newObject" >
{{ newObject }}
</ div>
</ div>
</ template>
< script>
export default {
data ( ) {
return {
tableData : [
{ key : "name" , value : "张三" } ,
{ key : "age" , value : "18" } ,
{ key : "phone" , value : "153****0000" } ,
{ key : "idCard" , value : "100000********0000" } ,
] ,
num : 0 ,
newObject : null ,
} ;
} ,
watch : { } ,
mounted ( ) { } ,
methods : {
operate ( ) {
let tempArr = [
{ key : "link" , value : "http://XXX.XX.XX" } ,
{ key : "image" , value : "XXX.png" } ,
] ;
this . newObject = this . tableData. concat ( ... tempArr) ;
this . num += 1 ;
} ,
} ,
} ;
</ script>
操作前
操作后