1-1 filter 过滤关键字
<script>
/* 返回数组
String match
*/
var arr = [{city:"武汉",state:true,spell:"wuhan"},
{city:"北京",state:false,spell:"beijing"},
{city:"武昌",state:false,spell:"wuchang"}]
/* city中含有武的组成一个数组 */
var keyword = "武汉";
var res = arr.filter(item=>{
// return item.city.includes(keyword);
if(item.city.includes(keyword) || item.spell.includes(keyword) ){
return item;
}
})
console.log(res)
</script>
1-2 数组去重
<script>
/* 数组去重
*/
var arr = [1,2,1,4,5,6,4];
/* arr = [1,2,4,5,6] */
var result = [];
arr.forEach(item=>{
if(!result.includes(item)){
result.push(item);
}
})
console.log(result)
</script>
1-3 对象中键的数组过滤
var obj ={
A:[{name:"html"},{name:"ctc"}],
B:[{name:"vue"},{name:"js"}],
C:[{name:"vue"},{name:"js"}]
}
var keyword ="c";
/* arr */
var arr = Object.values(obj);
var res = [];
arr.forEach(item=>{
res.push(...item)
})
console.log(res)