美文网首页
JS数组那些用法

JS数组那些用法

作者: 废弃的种子 | 来源:发表于2021-04-14 12:24 被阅读0次

数组的Array.prototype.slice.call()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div id="app">
    <p v-for="count in 10">第{{count}}次, 总计 10次</p>

    <ul ref="ulParent">
        <li style="height: 60px;">1</li>
        <li style="height: 30px;">2</li>
        <li style="height: 50px;">3</li>
        <li style="height: 160px;">4</li>
        <li style="height: 10px;">5</li>
    </ul>

    <button @click="getAllLiHeight()">获取高度数组</button>
    <button @click="dealSome()">验证some</button>
</div>
<script src="lib/vue.js"></script>
<script>
    new Vue({
        el:'#app',
        data: {
           dataArr: ['123213', '123213.com', '哈哈哈哈']
        },
        methods: {
            getAllLiHeight(){
                let liHeightArr = [];
                // 1. 获取dom元素
                let allLis = this.$refs.ulParent.getElementsByTagName('li');
                console.log(allLis[0].clientHeight);
                console.log(Array.prototype.slice.call(allLis));
                Array.prototype.slice.call(allLis).forEach(li => {
                    liHeightArr.push(li.clientHeight);
                });
                console.log(liHeightArr);
            },

            dealSome(){
                let result = this.dataArr.some((str)=>{
                    return str === '哈哈哈哈';
                });
                console.log(result);
            }
        }
    });
    var obj1 = {0:'hello',1:'world'};
    var obj2 = {0:'hello',1:'world',length:2};
    var obj3 = [1,2];
    var obj4 = "string";
console.log(Array.prototype.slice.call(obj4,1));//第二个参数相当于截取得起始下标
//Array.prototype.slice  调用Array类中的饿原型方法
//Array.prototype.slice.call(res)//使用call冒充一个据有length对象,转换成数组,绑定this指向——>对象.slice() 或者  字符.slice() 或者  数组.slice() ——>第二个参数为截取的起始下标
//作用:将传入具有length属性的参数转为数组   
</script>
</body>
</html>

数组的fill方法

new Array(3).fill({}).map(()=>{return {name:'小明'}})
![image.png](https://img.haomeiwen.com/i20805121/412bcda0abbc1b43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

网友评论

      本文标题:JS数组那些用法

      本文链接:https://www.haomeiwen.com/subject/vxsgwktx.html