JavaScript进阶 - ES5新增的方法


一、数组方法

迭代方法 : forEach(),map(),filter(),some(),every()。

1. forEach()

语法 :

array.forEach(function(currentValue, index, arr))
  • currentValue : 数组当前项的值
  • index :数组当前项的索引
  • arr :数组对象本身
<script>
    var arr = [1, 2, 3];
    arr.forEach(function (value, index, array) {
        console.log('每个数组元素:' + value);
        console.log('每个数组元素的索引:' + index);
        console.log('数组本身:' + array);
    })
</script>

2. filter()

语法 :

array.filter(function(currentValue, index, arr))

filter()方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素,主要用于筛选数组。
它直接返回一个新数组

  • currentValue:数组当前项的值
  • index:数组当前项的索引
  • arr:数组对象本身
<script>
    var arr = [12, 34, 5, 6, 43, 22]
    var newArr = arr.filter(function (value, index) {
        return value >= 20;
    })
    console.log(newArr);  //  [34, 43, 22]
</script>

3. some()

语法 :

array.some(function(currentValue, index, arr))

查找数组中是否有满足条件的元素
它返回值是布尔值,如果查找到这个元素,就返回true,如果查找不到就返回false。

<script>
   	var arr = [12, 34, 5, 6, 43, 22]
    var newArr = arr.some(function (value) {
        return value >= 20;
    })
    console.log(newArr);  //  true
</script>

查询商品案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 600px;
            margin: 50px auto;
        }

        .box span input {
            width: 50px;
        }

        table {
            width: 400px;
            text-align: center;
            border: 1px solid black;
            margin-top: 50px;
        }
    </style>
</head>

<body>
    <div class="box">
        <span>
            按照价格查询:<input type="text" class="start">-<input type="text" class="end">
            <button class="search-price">搜索</button>
            按照商品名称查询:<input type="text" class="product">
            <button class="search-name">查询</button>
        </span>
        <table>
            <thead>
                <th>id</th>
                <th>产品名称</th>
                <th>价格</th>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>

    <script>
        var data = [{
            id: 1,
            pname: '小米',
            price: 2999
        }, {
            id: 2,
            pname: 'oppo',
            price: 3999
        }, {
            id: 3,
            pname: '荣耀',
            price: 1999
        }, {
            id: 4,
            pname: '华为',
            price: 1299
        }];

        // 1. 获取元素
        var tbody = document.querySelector('tbody');
        var start = document.querySelector('.start');
        var end = document.querySelector('.end');
        var product = document.querySelector('.product');
        var search_price = document.querySelector('.search-price');
        var search_name = document.querySelector('.search-name');

        // 渲染页面
        function setData(mydata) {
            tbody.innerHTML = '';
            // 2. 遍历
            mydata.forEach(function (value) {
                var tr = document.createElement('tr');
                tr.innerHTML = '<td>' + value.id + '</td><td>' + value.pname + '</td><td>' + value.price + '</td>'
                tbody.appendChild(tr);
            })
        }
        setData(data);

        // 3. 根据价格筛选
        search_price.addEventListener('click', function () {
            var newData = data.filter(function (value) {
                return value.price >= start.value && value.price <= end.value;
            })
            setData(newData);
        })

        // 4. 根据名称查询
        search_name.addEventListener('click', function () {
            var arr = [];
            data.some(function (value) {
                if (value.pname == product.value) {
                    arr.push(value);
                }
            });
            setData(arr);
        })
    </script>
</body>

</html>

二、字符串方法

trim()方法会从一个字符串的两端删除空白字符

str.trim();

三、对象方法

Object.defineProperty()定义对象中新属性或修改原有的属性。
是 vue的响应式原理

Object.defineProperty(obj, prop, descriptor)
  • obj :必需,目标对象
  • prop :必需,需定义或修改的属性的名字
  • descriptor :必需,目标属性所拥有的特性
    • descriptor以对象形式书写
    • value :设置属性的值,默认为undefined
    • writable :值是否可以重写。true | false
    • enumerate :目标属性是否可以被枚举。 true | false
    • configurable :目标属性是否可以被删除或是否可以再次修改特性。true | false。
<script>
    var obj = {
        id: 1,
        pname: '小米',
        price: 10000
    };
    Object.defineProperty(obj, 'num', {
        value: 10000
    })
    console.log(obj);
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值