let answers=[];
answers=answers.concat(data.list); // 数组连接
// 通过数组中元素的某个字段排序:
this.answerInfo = answers.sort((t1,t2)=>{
return t1.questionIndex-t2.questionIndex;
});
删除数组索引是 i 的 元素: arr.splice(i,1)
var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" arr.splice(3,1) console.log(arr) 输出: [ 'George', 'John', 'Thomas', 'Adrew', 'Martin' ]
三: 数组 filter
let answer = []; let answerText = ""; let add = { answerText: "" }; add.answerText = "A,C"; if (add.answerText) { answerText = add.answerText.split(","); console.log(add.answerText) console.log(answerText) answerText.filter((item) => { answer.push({ text: item }); }); console.log(answer) } 输出: A,C [ 'A', 'C' ] [ { text: 'A' }, { text: 'C' } ]
四: 获取文件名:
let file="www.baidu.com/test/abc.jpg" let filename=file.substr(file.lastIndexOf("/")+1) console.log(filename) 输出: abc.jpg
五: 数组去重:
var temp = ["grade.1", "grade.2","grade.1"]
var uniArr = uniqueArr(temp)
console.log(uniArr) // 输出 ["grade.1", "grade.2"]
function uniqueArr(arr) {
return Array.from(new Set(arr))
}
六: 排序:
var temp = [1, 3,4,2]
temp.sort(function(a, b){return a - b});