删除/获取某个数组的多个指定元素

93 阅读2分钟
  • 获取某个数组的多个指定元素
// 首先先定义三个数组,这里的原始数组选择了较为复杂的对象式数组
options: [
        {
          value: "0",
          label: "入党申请材料",
        },
        {
          value: "1",
          label: "入党积极分子材料",
        },
        {
          value: "2",
          label: "政审材料",
        },
        {
          value: "3",
          label: "转正材料",
        },
        {
          value: "4",
          label: "入党志愿书",
        },
        {
          value: "5",
          label: "转出证明",
        },
        {
          value: "6",
          label: "死亡证明",
        },
        {
          value: "7",
          label: "开除党籍证明",
        },
        {
          value: "8",
          label: "其他材料",
        },
      ],
      typeList: [],
      delTypeList: [],

通过后端接口获取数据获取数据:

//获取成功后
this.typeList = data.typelist; //后端获取的是JSON字符串格式,所以可以直接赋值

获取指定的数据

this.typeList = [0,1,2,3,4]
var item = "";
for (item of this.typeList) {
    this.delTypeList.push(this.options[item]);
}
this.delTypeList: [
        {
          value: "0",
          label: "入党申请材料",
        },
        {
          value: "1",
          label: "入党积极分子材料",
        },
        {
          value: "2",
          label: "政审材料",
        },
        {
          value: "3",
          label: "转正材料",
        },
        {
          value: "4",
          label: "入党志愿书",
        },
 ]

-获取某个数组的多个指定元素

// 首先先定义三个数组,这里的原始数组选择了较为复杂的对象式数组
options: [
        {
          value: "0",
          label: "入党申请材料",
        },
        {
          value: "1",
          label: "入党积极分子材料",
        },
        {
          value: "2",
          label: "政审材料",
        },
        {
          value: "3",
          label: "转正材料",
        },
        {
          value: "4",
          label: "入党志愿书",
        },
        {
          value: "5",
          label: "转出证明",
        },
        {
          value: "6",
          label: "死亡证明",
        },
        {
          value: "7",
          label: "开除党籍证明",
        },
        {
          value: "8",
          label: "其他材料",
        },
      ],
      typeList: [],
      addTypeList: [],

不能直接赋值,因为不是字符串格式。

this.addTypeList = this.options.concat(); //把options的值复制给addTypeList列表

通过后端接口获取数据获取数据:

//获取成功后
this.typeList = data.typelist; //后端获取的是JSON字符串格式,所以可以直接赋值

删除指定的数据后的数组

var item = "";
//splice --删除任意数量的项,只需要传入两个参数即可。 要删除的第一项的位置和要删除的项数
//indexOf --`indexOf()` 方法返回值在字符串中第一次出现的位置
for (item of this.typeList) {
    this.addTypeList.splice(
        this.addTypeList.indexOf(this.options[item]),
        1
        );
    }
this.addTypeList:[
        {
          value: "5",
          label: "转出证明",
        },
        {
          value: "6",
          label: "死亡证明",
        },
        {
          value: "7",
          label: "开除党籍证明",
        },
        {
          value: "8",
          label: "其他材料",
        },
      ],