数组常用方法总结 (1) :pop / push / shift / unshift

本文详细介绍了JavaScript中数组的四个常用方法:pop、push、shift和unshift。pop用于从数组尾部删除一项并返回该值;push在尾部添加元素并返回新长度;shift移除数组开头的一项并返回移除的值;unshift在数组开头添加元素并返回新长度。这些方法都会改变原数组。
摘要由CSDN通过智能技术生成

pop

  • 从尾部删除一项。
  • 改变原有数组。
  • 返回删除掉的内容。
<template>
    <div class="myBlock">
        <div class="tableBlock">
            <div class="title">
                {{ newObject ? "操作后的数组" : "操作前的数组" }}
            </div>
            <el-table :data="tableData" :key="num" stripe border>
                <el-table-column prop="name" label="姓名" width="200">
                </el-table-column>
                <el-table-column prop="phone" label="电话" width="200">
                </el-table-column>
                <el-table-column prop="idCard" label="证件号">
                </el-table-column>
            </el-table>
        </div>
        <div class="operate">
            <el-button type="success" plain @click="operate">
                pop
            </el-button>
        </div>
        <div class="title" v-if="newObject">操作后的返回值</div>
        <div class="newObject">
            {{ newObject }}
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [],
            num: 0,
            newObject: null,
        };
    },
    watch: {},
    mounted() {
    	// 渲染假数据
        for (var i = 1; i < 4; i++) {
            this.tableData.push({
                name: "员工" + i + "号",
                phone: "153****0000",
                idCard: "1111**********0000",
            });
        }
    },
    methods: {
        operate() {
            this.newObject = this.tableData.pop();
            // 更新 table 节点,可以直观看到原有数组是否改变
            this.num += 1;
        },
    },
};
</script>

操作前

操作前

操作后

操作后

push

  • 从尾部添加一项。
  • 改变原有数组。
  • 返回操作后的数组的长度。
<template>
    <div class="myBlock">
        <div class="tableBlock">
            <div class="title">
                {{ newObject ? "操作后的数组" : "操作前的数组" }}
            </div>
            <el-table :data="tableData" :key="num" stripe border>
                <el-table-column prop="name" label="姓名" width="200">
                </el-table-column>
                <el-table-column prop="phone" label="电话" width="200">
                </el-table-column>
                <el-table-column prop="idCard" label="证件号">
                </el-table-column>
            </el-table>
        </div>
        <div class="operate">
            <el-button type="success" plain @click="operate">
                push
            </el-button>
        </div>
        <div class="title" v-if="newObject">操作后的返回值</div>
        <div class="newObject">
            {{ newObject }}
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [],
            num: 0,
            newObject: null,
        };
    },
    watch: {},
    mounted() {
    	// 渲染假数据
        for (var i = 1; i < 4; i++) {
            this.tableData.push({
                name: "员工" + i + "号",
                phone: "153****0000",
                idCard: "1111**********0000",
            });
        }
    },
    methods: {
        operate() {
            this.newObject = this.tableData.push({
                name: "test",
                phone: "testPhone",
                idCard: "testIdCars",
            });
            // 更新 table 节点,可以直观看到原有数组是否改变
            this.num += 1;
        },
    },
};
</script>

操作前

操作前

操作后

操作后

shift

  • 从头部删除一项。
  • 改变原有数组。
  • 返回删除掉的内容。
<template>
    <div class="myBlock">
        <div class="tableBlock">
            <div class="title">
                {{ newObject ? "操作后的数组" : "操作前的数组" }}
            </div>
            <el-table :data="tableData" :key="num" stripe border>
                <el-table-column prop="name" label="姓名" width="200">
                </el-table-column>
                <el-table-column prop="phone" label="电话" width="200">
                </el-table-column>
                <el-table-column prop="idCard" label="证件号">
                </el-table-column>
            </el-table>
        </div>
        <div class="operate">
            <el-button type="success" plain @click="operate">
                shift
            </el-button>
        </div>
        <div class="title" v-if="newObject">操作后的返回值</div>
        <div class="newObject">
            {{ newObject }}
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [],
            num: 0,
            newObject: null,
        };
    },
    watch: {},
    mounted() {
    	// 渲染假数据
        for (var i = 1; i < 4; i++) {
            this.tableData.push({
                name: "员工" + i + "号",
                phone: "153****0000",
                idCard: "1111**********0000",
            });
        }
    },
    methods: {
        operate() {
            this.newObject = this.tableData.shift();
            // 更新 table 节点,可以直观看到原有数组是否改变
            this.num += 1;
        },
    },
};
</script>

操作前

操作前

操作后

操作后

unshift

  • 从头部添加一项。
  • 改变原有数组。
  • 返回添加后的数组的长度。
<template>
    <div class="myBlock">
        <div class="tableBlock">
            <div class="title">
                {{ newObject ? "操作后的数组" : "操作前的数组" }}
            </div>
            <el-table :data="tableData" :key="num" stripe border>
                <el-table-column prop="name" label="姓名" width="200">
                </el-table-column>
                <el-table-column prop="phone" label="电话" width="200">
                </el-table-column>
                <el-table-column prop="idCard" label="证件号">
                </el-table-column>
            </el-table>
        </div>
        <div class="operate">
            <el-button type="success" plain @click="operate">
                unshift
            </el-button>
        </div>
        <div class="title" v-if="newObject">操作后的返回值</div>
        <div class="newObject">
            {{ newObject }}
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            tableData: [],
            num: 0,
            newObject: null,
        };
    },
    watch: {},
    mounted() {
    	// 渲染假数据
        for (var i = 1; i < 4; i++) {
            this.tableData.push({
                name: "员工" + i + "号",
                phone: "153****0000",
                idCard: "1111**********0000",
            });
        }
    },
    methods: {
        operate() {
            this.newObject = this.tableData.unshift({
                name: "test",
                phone: "testPhone",
                idCard: "testIdCars",
            });
            // 更新 table 节点,可以直观看到原有数组是否改变
            this.num += 1;
        },
    },
};
</script>

操作前

操作前

操作后

操作后

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值