1. var arr =[
  2. {
  3. name:"lisi",
  4. age:18,
  5. likes:["lol","足球"],
  6. alikes:["音乐","电影"],
  7. sex:"male"
  8. },
  9. {
  10. name:"王五",
  11. age:17,
  12. likes:["ol","篮球"],
  13. alikes:["函数","事件"],
  14. sex:"male"
  15. },
  16. {
  17. name:"张三",
  18. age:165,
  19. likes:["lo22","足球1"],
  20. alikes:["音乐2","电影3"],
  21. sex:"male"
  22. },
  23. ]
  24. var res = [];
  25. for(var i =0;i<arr.length;i++){
  26. // console.log(arr[i])
  27. var name = arr[i].name;
  28. var age = arr[i].age;
  29. var likes = arr[i].likes.concat(arr[i].alikes);
  30. var alikes = arr[i].alikes.concat(arr[i].alikes);
  31. res.push(
  32. {
  33. name:name,
  34. age:age,
  35. likes:likes
  36. }
  37. )
  38. }
  39. console.log(res)

示例2

  1. es6方法
  2. var arr =[
  3. {
  4. name:"lisi",
  5. age:18,
  6. likes:["lol","足球"],
  7. alikes:["音乐","电影"],
  8. sex:"male"
  9. },
  10. {
  11. name:"王五",
  12. age:17,
  13. likes:["ol","篮球"],
  14. alikes:["函数","事件"],
  15. sex:"male"
  16. },
  17. {
  18. name:"张三",
  19. age:165,
  20. likes:["lo22","足球1"],
  21. alikes:["音乐2","电影3"],
  22. sex:"male"
  23. },
  24. ]
  25. var res = [];
  26. for(var i =0;i<arr.length;i++){
  27. //解构
  28. var {name,age,likes,alikes} = arr[i];
  29. likes.push(...alikes);
  30. //键和值相同只用写一个
  31. var obj={
  32. name,
  33. age,
  34. likes
  35. }
  36. res.push(obj);
  37. }
  38. console.log(res)

数组中搜索值进行排序,至于顶部

  1. //让数组中被搜索的值,至于顶部
  2. var arr =["html","css","js","vue","react"]
  3. $("#app").keyup(function(event){
  4. if(event.keyCode ==13){
  5. /*将值添加到一个数组里面去*/
  6. var value = $(this).val();
  7. if(arr.indexOf(value)!=-1){
  8. console.log(value)
  9. var index = arr.indexOf(value)
  10. arr.splice(index,1);
  11. arr.unshift(value);
  12. console.log(arr)
  13. }
  14. }
  15. })

字符串请求模板示例

  1. var arr = [
  2. {
  3. imgUrl:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg1.doubanio.com%2Fview%2Fthing_review%2Fl%2Fpublic%2Fp6346199.jpg&refer=http%3A%2F%2Fimg1.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1636337497&t=5cf8473b04e7343e539e7d8d4461afbf",
  4. name:"cheng",
  5. age:19
  6. },{
  7. imgUrl:"https://gss0.baidu.com/70cFfyinKgQFm2e88IuM_a/baike/pic/item/0e2442a7d933c8952e6a4f65d81373f08202009c.jpg",
  8. name:"lisi",
  9. age:18
  10. }
  11. ]
  12. var res = [];
  13. for(var i=0;i<arr.length;i++){
  14. //console.log(arr[i])
  15. var {imgUrl,name,age} = arr[i];
  16. var template = `
  17. <div class="item">
  18. <p>${name}<p>
  19. <img src="${imgUrl}"/>
  20. <p>${age}</p>
  21. </div>
  22. `
  23. console.log(template)
  24. $("body").prepend(template)
  25. }

获取id下标的值

  1. <input type="text" id="app">
  2. <script>
  3. var arr = [
  4. {name:"李四",id:1001},
  5. {name:"王四",id:1002},
  6. {name:"张四",id:1003},
  7. {name:"赵四",id:1004},
  8. ]
  9. var app = document.getElementById("app")
  10. app.onkeydown = function(event){
  11. if(event.keyCode ===13){
  12. //console.log(this.value)
  13. var id = Number(this.value);
  14. //获取id的下标值
  15. //console.log(id);
  16. var index = arr.findIndex(item=>{
  17. return item.id ==id;
  18. })
  19. arr.splice(index,1);
  20. console.log(arr)
  21. }
  22. }

获取键盘输入框的下标数字

  1. <input type="text" id="app">
  2. <script>
  3. var keyword = "李"
  4. var students = [
  5. {name:"李四",age:"18岁"},
  6. {name:"李二",age:"11岁"},
  7. {name:"李五",age:"19岁"},
  8. {name:"王四",age:"22.5岁"},
  9. ]
  10. var app = document.getElementById("app")
  11. app.onkeydown = function(event){
  12. if(event.keyCode ===13){
  13. //获取键盘输入框的下标数字
  14. var keyCode = this.value;
  15. console.log(keyword)
  16. var res = students.filter(item=>{
  17. return item.name.includes(keyword)
  18. })
  19. console.log(res)
  20. }
  21. }

遍历方法

  1. //for in
  2. var arr = ["html","css","js"];
  3. for(var key in arr){
  4. console.log(key)//key获取的是数组的下标
  5. //console.log(arr[key])
  6. }
  7. var obj = {
  8. name:"list",
  9. age:18
  10. }
  11. for(var k in obj){
  12. console.log(obj[k])
  13. }

reduce方法

  1. var arr = [{
  2. name:"肯德基",price:100,distance:100,num:2
  3. },
  4. {
  5. name:"必胜客",price:200,distance:50,num:1
  6. },
  7. {
  8. name:"华莱士",price:50,distance:1000,num:5
  9. }
  10. ]
  11. var sum = arr.reduce(function(a,b){
  12. console.log(a);
  13. console.log(b);
  14. })

选择示例option

  1. <select id="mySelect">
  2. <option value="price">低价优先</option>
  3. <option value="distance">距离优先</option>
  4. </select>
  5. <script>
  6. var arr = [{
  7. name:"肯德基",price:100,distance:100
  8. },
  9. {
  10. name:"必胜客",price:200,distance:50
  11. },
  12. {
  13. name:"华莱士",price:50,distance:1000
  14. }
  15. ]
  16. var mySelect = document.getElementById("mySelect");
  17. //console.log(mySelect)
  18. mySelect.onchange = function(){
  19. console.log(this.value);
  20. var value = this.value;
  21. arr.sort(function(a,b){
  22. return a[value]-b[value];
  23. })
  24. console.log(arr)
  25. }

根据价格升序排序

  1. var arr = [{
  2. name:"肯德基",price:100,distance:100
  3. },
  4. {
  5. name:"必胜客",price:200,distance:50
  6. },
  7. {
  8. name:"华莱士",price:50,distance:1000
  9. }
  10. ]
  11. //根据价格升序
  12. arr.sort(function(a,b){
  13. return a.price-b.price;
  14. })
  15. console.log(arr)
  16. // 根据距离进行降序
  17. arr.sort(function(a,b){
  18. return b.distance-a.distance
  19. })
  20. console.log(arr)

sort方法

  1. var arr = [12,3,5,11,1];
  2. console.log(arr.sort())
  3. var newArr = arr.sort(function(a,b){
  4. return a-b;
  5. })
  6. var c = [12,45,2,3,5];
  7. var xjj = c.sort(function(a,b){
  8. return b-a;
  9. })
  10. console.log(newArr)
  11. console.log(xjj)

join方法连接

  1. <div id="app"></div>
  2. <script>
  3. var dates = ["2019","03","09"]
  4. var str = dates.join("-");
  5. console.log(str)
  6. var app = document.getElementById("app")
  7. app.innerHTML = str;

casts打印在css样式中的方法

  1. <p id="app"></p>
  2. <script>
  3. var directs = [
  4. {name:"吴京",id:1001},
  5. {name:"章子怡",id:1002},
  6. {name:"徐峥",id:1003}
  7. ]
  8. var arr = [];
  9. for(var i = 0;i<directs.length;i++){
  10. console.log(directs[i].name)
  11. arr.push(directs[i].name)
  12. }
  13. console.log(arr)
  14. var str = arr.join("/")
  15. console.log(str)
  16. var app = document.getElementById("app");
  17. app.innerHTML = str;