1-1 数组的方法

  1. var arr = [1,2,3,4,5,7] //[1,2,7]
  2. arr.splice(2,3)
  3. console.log(arr);
  4. var arr = [1,2,3,4,5,7] //[1,8,9,7]
  5. arr.splice(1,4,8,9)
  6. console.log(arr);

1-2 ==运算

  1. // Number(""),Number(" "),Number(null) => 输出 0
  2. console.log(false == ""); // true
  3. console.log(true == 1); // true
  4. console.log(null == undefined); // true
  5. console.log(undefined == false); // false
  6. console.log(1 == "1"); // true
  7. /* 1.只有有一边为 boolean 先两边转为number
  8. 2.一边为string,一边为number,先将两遍转为number
  9. // 两边都先转为number
  10. 3. null == undefined
  11. 4. null 和 undefined 不能转化为其他值
  12. */

1-3 join 练习

  1. var data ={
  2. country:"美国",
  3. year:1994,
  4. story:"犯罪"
  5. }
  6. // 美国/1994/犯罪
  7. /* 先将对象转为数组 在进行字符串拼接*/
  8. var arr = []
  9. for(var i in data){
  10. arr.push(data[i])
  11. }
  12. var str = arr.join("/")
  13. console.log(str);

1-4 sort 练习

1-4-1 练习1 根据年龄升序、降序

  1. var students = [
  2. {name:"zhang",age:20},
  3. {name:"li",age:18},
  4. {name:"wang",age:9},
  5. {name:"chen",age:14}
  6. ]
  7. /*
  8. 根据年龄升序
  9. 根据年龄降序
  10. */
  11. var result = students.sort((a,b)=>{
  12. return a["age"]-b["age"]
  13. })
  14. console.log(result);
  15. var result2 = students.sort((a,b)=>{
  16. return b.age-a.age
  17. })
  18. console.log(result2);

1-4-2 练习2 根据相应的值排序

  1. <button id="like">通过好评数排序</button>
  2. <button id="price">通过价格排序</button>
  3. <script>
  4. var like = document.getElementById("like")
  5. var price = document.getElementById("price")
  6. var sales = [
  7. {name:"张三",like:40,distance:1000,price:10},
  8. {name:"李四",like:60,distance:500,price:50},
  9. {name:"王五",like:30,distance:600,price:40}
  10. ]
  11. /* 升序 */
  12. function addOrder(arr,value){
  13. if(Array.isArray(arr)){
  14. var str = arr.sort((a,b)=>{
  15. return a[value]-b[value]
  16. })
  17. return str
  18. }else{
  19. return null
  20. }
  21. }
  22. like.onclick = function(){
  23. console.log(addOrder(sales,"like"));
  24. }
  25. price.onclick = function(){
  26. console.log(addOrder(sales,"price"));
  27. }
  28. </script>

1-5 map 练习

  1. var arr = [
  2. {name:"html",value:"12px"},
  3. {name:"css",value:"13px"},
  4. {name:"vue",value:"11px"}
  5. ]
  6. arr.map(item =>{
  7. var result = parseInt(item["value"])*2
  8. item["value"] = result
  9. })
  10. console.log(arr);
  11. /* {name: "html", value: 24}
  12. {name: "css", value: 26}
  13. {name: "vue", value: 22} */

1-6 filter 练习

  1. var arr = [
  2. {name:"小米",price:1999},
  3. {name:"红米",price:999},
  4. {name:"iphone",price:9999},
  5. {name:"华为",price:3000},
  6. ]
  7. var res = arr.filter(item =>{
  8. return item["price"]>1000
  9. })
  10. console.log(res);

1-7 输入框获取对应数据

  1. <input type="text" id="input">
  2. <script>
  3. var arr = [
  4. {name:"你是谁,你在哪",id:1102},
  5. {name:"圈住你",id:1102},
  6. {name:"hello在哪里",id:1102},
  7. {name:"怪你",id:1102},
  8. {name:"world",id:1102}
  9. ]
  10. /*
  11. 1.enter回车获取关键字
  12. 2.使用关键字,对数据查询,得到一个新数组
  13. 3.name的字符超过3位以...结尾
  14. */
  15. /* 1.enter回车获取关键字 */
  16. var input = document.getElementById("input")
  17. input.onkeydown = function(event){
  18. if(event.keyCode == 13){
  19. var value = this.value
  20. /* 2.使用关键字,对数据查询,得到一个新数组 */
  21. if(value){
  22. var res = arr.filter(item =>{
  23. return item["name"].includes(value)
  24. })
  25. /* 3.name的字符超过3位以...结尾 */
  26. res.map(item=>{
  27. if(item.name.length>3){
  28. item.name=item.name.slice(0,3)+"..."
  29. }
  30. })
  31. console.log(res);
  32. }
  33. }
  34. }
  35. </script>

1-8 分割数组

  1. var arr = [1,2,3,4,5,6,7,8,9]
  2. // [1,2,3] [4,5,6] [7,8,9]
  3. // 0 2
  4. // 3 5
  5. // 6 8
  6. var sum = []
  7. for(var i =0;i<arr.length;i=i+3){
  8. var item = arr.slice(i,i+3)
  9. sum.push(item)
  10. }
  11. console.log(sum);