[javascript] JS 物件陣列篩選 update es6 version
用法如下
var School = [
{ID:1,Name:'台南市和順國中'},
{ID:2,Name:'台南市安順國中'},
{ID:3,Name:'台南市安南國中'},
];
var filterSchool = [];
filterSchool = School.filter(function (element, index, array) {
var patt1 = new RegExp('安');
return patt1.test(element.Name);
});
console.log(filterSchool)
-- 更新ES6寫法
constSchool = [
{ID:1,Name:'台南市和順國中'},
{ID:2,Name:'台南市安順國中'},
{ID:3,Name:'台南市安南國中'},
];
let filterSchool = [];
let keyword = '安';
filterSchool = School.filter( (element, index, array) => {
const patt1 = new RegExp(this.keyword );
return patt1.test(element.Name);
});
補充
RegExp
正規表示式
var School = [
{ID:1,Name:'台南市和順國中'},
{ID:2,Name:'台南市安順國中'},
{ID:3,Name:'台南市安南國中'},
var filterSchool = [];
filterSchool = School.filter(function (element, index, array) {
var patt1 = new RegExp('安');
return patt1.test(element.Name);
});
console.log(filterSchool)
-- 更新ES6寫法
constSchool = [
{ID:1,Name:'台南市和順國中'},
{ID:2,Name:'台南市安順國中'},
{ID:3,Name:'台南市安南國中'},
let filterSchool = [];
let keyword = '安';
filterSchool = School.filter( (element, index, array) => {
const patt1 = new RegExp(this.keyword );
return patt1.test(element.Name);
});
補充
RegExp
正規表示式
留言
張貼留言