1、es6解构-展开-键值相同
<script>
var arr = [
{
name:"lisi",
age:18,
likes:["lol","足球"],
alikes:["音乐","电影"],
sex:"male"
},
{
name:"王五",
age:17,
likes:["al","栏球"],
alikes:["防抖","放到"],
sex:"male"
},
{
name:"张三",
age:165,
likes:["发顺丰","jf"],
alikes:["音乐9","10电影"],
sex:"male"
}
]
var res = [];
for(var i=0;i<arr.length;i++){
/* 解构 */
var {name,age,likes,alikes} = arr[i];
/* 展开 */
likes.push(...alikes);
/* 键和值相同只用写一个 */
var obj ={
name,
age,
likes
}
res.push(obj);
}
console.log(res)
</script>
2.让数组中被搜索的值,至于顶部
<script>
/* 让数组中被搜索的值,至于顶部 */
var arr= ["html","css","js","vue","react"]
/* ["js","html","css","vue","react"] */
$("#app").keyup( function(event){
if(event.keyCode==13){
var value = $(this).val();
if(arr.indexOf(value)!=-1){
console.log(value)
var index = arr.indexOf(value);
arr.splice(index,1);
arr.unshift(value);
console.log(arr);
}
}
})
</script>
3.历史搜索
<script>
/* 1、获取输入框的值 */
var historys = [];
$("#app").keyup(function (event) {
if (event.keyCode == 13) {
/* 2.将值添加到一个数组里面去 */
var value = $(this).val();
var template = `
<button>${value}</button>
`
/* 只有数组中不包含输入的关键字才向数组添加 */
if (value) {
if (!historys.includes(value)) {
historys.unshift(value);
/* 3、渲染数据到页面 */
$("#container").prepend(template)
$(this).val("")
} else {
var index = historys.indexOf(value);
var res = historys.splice(index, 1);
historys.unshift(...res);
console.log(historys)
$("button").eq(index).remove();
$("#container").prepend(template)
$(this).val("")
}
}
}
})
</script>