数组部分操作(二)

 unshift() 方法向数组插入一个对象或者字符串

  const dataList = [
      {dictValue: "BAO_GAN", dictLabel: "包干拉货", dictType: "contract_type", dictSort: 2},
      {dictValue: "JI_LIANG", dictLabel: "计量拉货", dictType: "contract_type", dictSort: 3},
      {dictValue: "YUE_JIE", dictLabel: "月结拉货", dictType: "contract_type", dictSort: 4},
      {dictValue: "ZI_XUN", dictLabel: "咨询服务", dictType: "contract_type", dictSort: 5}
    ]
    const text = {dictValue: "" ,dictLabel: "请选择原合同类型", dictType: "contract_type",dictSort: 0}
    dataList.unshift(text)
    console.log(contactData);
//(5) [{…}, {…}, {…}, {…}, {…}]
0: {dictLabel: "请选择原合同类型", dictSort: 0, dictType: "contract_type", dictValue: ""}
1: {dictValue: "BAO_GAN", dictLabel: "包干拉货", dictType: "contract_type", dictSort: 2}
2: {dictValue: "JI_LIANG", dictLabel: "计量拉货", dictType: "contract_type", dictSort: 3}
3: {dictValue: "YUE_JIE", dictLabel: "月结拉货", dictType: "contract_type", dictSort: 4}
4: {dictValue: "ZI_XUN", dictLabel: "咨询服务", dictType: "contract_type", dictSort: 5}

Array.map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,同时不会改变原来的数组

const list = [{id: 1, value: '123'},{id: 3, value: '123'},{id: 4, value: '123'},{id: 5, value: '123'},]
      let Arr = []
      Arr = list.map(item =>item.id)
      console.log(Arr)
//[1, 3, 4, 5]

push() :可向数组的末尾添加一个或多个元素,并返回新的长度。

const arr = [{key:1,id:2,age:3},{key:2,id:3,age:4},{key:3,id:4,age:5}]
      arr.push({
        key: arr.length+1
      })
      console.log(arr)
//(4) [{…}, {…}, {…}, {…}]
0: {key: 1, id: 2, age: 3}
1: {key: 2, id: 3, age: 4}
2: {key: 3, id: 4, age: 5}
3: {key: 4}

filter 
它用于把Array的某些元素过滤掉,然后返回剩下的元素。

var arr = [1, 2, 4, 5, 6, 9, 10, 15];
var list = arr.filter(function (x) {
    return x % 2 !== 0;
});
console.log(list); // [1, 5, 9, 15]

 const dataSource = [...this.industry]
 this.industry = dataSource.filter(item => item.wasteTradeId !== key.wasteTradeId)
只返回满足条件的

splice() //数组操作

有三种操作 删除 新增 替换

var array = ['a', 'b', 'c', 'd']
      // 1. 删除功能,第一个参数为第一项位置,第二个参数为要删除几个。
      // array.splice(index,num),返回值为删除内容,array为结果值
      array.splice(0, 2)
      console.log(array) // ['c', 'd']
      // 2. 插入功能,第一个参数(插入位置),第二个参数(0),第三个参数(插入的项)。
      // array.splice(index,0,insertValue)
      array.splice(0,0,'insertValue')
      console.log(array) // ['insertValue', 'a', 'b', 'c', 'd']
      // 3.替换功能,第一个参数(起始位置),第二个参数(删除的项数),第三个参数(插入任意数量的项)。
      // array.splice(index,num,insertValue)
      array.splice(0,1,'insertValue')
      console.log(array) // ['insertValue',  'a', 'b', 'c', 'd']

at() 访问元素

let arr = ['a', 'b', 'c', 'd']

console.log(arr.at(1)); //b

如果访问最后一个元素则是 arr.at(-1)

shift取出数组第一个元素

let arr = ['a','b','c', 'd'];

let first = arr.shift();

console.log(first); //a arr变为['b','c','d']

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值