1、以…结尾
1.1、超过六位
<script>
//超过六位以...结尾
var str = "时间有限来不及去惦记心里没有我的人"
console.log(str.length);
if(str.length>6){
var res = str.slice(0,6)+"..."; //var res = str.substr(0,6)+"...";
console.log(res);
}
</script>
1.2、超过三位
<script>
var arr = [
"时间有限来",
"及去惦记心里",
"及去惦记心里没有我的人",
"你好"
]
var res = [];
arr.forEach(item=>{
if(item.length>3){
item = item.substr(0,3)+"...";
}
res.push(item);
})
console.log(res);
</script>
1.3、map方法实现:
<script>
var arr = [
"时间有限来",
"及去惦记心里",
"及去惦记心里没有我的人",
"你好"
]
var res = arr.map(item=>{
if(item.length>3){
item = item.substr(0,3)+"...";
}
return item
})
console.log(res);
</script>
1.4、把含有“你”的字符串超过三位以…结尾
<input type="text" id="app">
<script>
var arr = [
"时间有限来",
"及去惦你记心里",
"及去惦记心里没有我的人",
"你好"
]
$("#app").keyup(function(event){
if(event.keyCode == 13){
var value = $(this).val();
var res = arr.filter(item=>{
return item.includes(value);
})
var lists = res.map(item=>{
if(item.length>3){
item = item.substr(0,3)+"...";
}
return item;
})
console.log(lists);
}
})
</script>
1.5、通过id搜索,最后以…结尾
<input type="text" id="app">
<script>
var arr = [
{id:1001,name:"fslkhlasl"},
{id:1002,name:"bkhaoiuohf"},
{id:1003,name:"hiuios"}
]
//1.获取input的值
$("#app").keyup(function (event) {
if (event.keyCode == 13) {
var value = $(this).val();
var res = arr.find(item=>{
return item.id == value;
})
res.name = res.name +"...";
var list = [res];
console.log(list);
}
})
</script>
2、字符串分割数组反转
<script>
var str = "hello";
var arr = str.split("")
console.log(arr);
arr.reverse();
console.log(arr.join(""));
</script>
3、?key=你&age=18
<script>
var str = "?key=你&age=18"
var res = str.slice(1);
var list = res.split("&");
console.log(list);
var key = list[0];
var age = list[1];
key = key.split("=")[1];
age = age.split("=")[1];
var obj = {
key,
age,
}
console.log(obj);
</script>
3.1、”?key=你&age=18&offset=10”
<script>
var list = [];
var str = "?key=你&age=18&offset=10";
//[{key:"你"},{age:18},{offset:10}]
var arr = str.slice(1).split("&");
arr.forEach(item=>{
var res = item.split("=");
var obj = {};
obj[res[0]]=res[1];
list.push(obj);
})
console.log(list);
</script>
3.2、map 方法 “?key=你&age=18&offset=10”
<script>
var str = "?key=你&age=18&offset=10";
var arr = str.slice(1).split("&");
console.log(arr);
var res = arr.map(item=>{
var obj = {};
var list = item.split("=");
obj[list[0]] = list[1];
console.log(obj);
return obj;
})
console.log(res);
</script>
3.3、”https://www.baidu.com?order=hot&cat=你&limit=35&offset=140“
<script>
var url = "https://www.baidu.com?order=hot&cat=你&limit=35&offset=140";
var res = url.split("?")[1].split("&");
console.log(res);
var obj = {};
//让数组的每一项中,第一项作为key,第二项作为value
res.forEach(item=>{
var list = item.split("=");
obj[list[0]] = list[1];
})
console.log(obj);
</script>