js-数组

一、解析动态json数据

 取出key和value的值。再封装到一个数组里面。 

let userData = res.data.data;
let userArray=[];
for (let [key, value] of Object.entries(userData)) {
  userArray.push({"时间":key,"人数":value});
  console.log(userArray)
}

只取key值:

for (let key of Object.keys(obj)) {
  
}

只取value值:

for (let value of Object.values(obj)) {
  
}

二、去掉指定数组对象 id 相同的元素

去除数组中 id 等于 2 的元素

arr.splice(
  arr.findIndex((item) => item.id == 2),
  1
);

三、过滤对象数组中 key 值相同的数据

const res = new Map();
const new2 = arr.filter(
  (item) => !res.has(item.month) && res.set(item.month, 1)
);
console.log(arr.length, new2);
//6 "[{"count":1095,"month":"三月"},{"count":987,"month":"四月"},{"count":753,"month":"五月"}]"

 四、数组去重

方法一、

let arr = [1, 2, 2, 3, 3, 4, 4, 5, 6];
let newArr = [...new Set(arr)];
//结果:newArr = [1, 2, 3, 4, 5, 6]

方法二、

let arr = [1, 2, 2, 3, 3, 4, 4, 5, 6];
for(let i=0;i<arr.length;i++){
	for(let j=i+1;j<arr.length;j++){
		if(arr[i]==arr[j]){
			arr.splice(j,1);
			j--;
		}
	}
}

五、随机取数组对象

传入方法的两个参数:

//arr为数组,num为想要取出数据个数。
function getArrayItems(arr, num) {
  //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组;
  var temp_array = new Array();
  for (var index in arr) {
    temp_array.push(arr[index]);
  }
  //取出的数值项,保存在此数组
  var return_array = new Array();
  for (var i = 0; i < num; i++) {
    //判断如果数组还有可以取出的元素,以防下标越界
    if (temp_array.length > 0) {
      //在数组中产生一个随机索引
      var arrIndex = Math.floor(Math.random() * temp_array.length);
      //将此随机索引的对应的数组元素值复制出来
      return_array[i] = temp_array[arrIndex];
      //然后删掉此索引的数组元素,这时候temp_array变为新的数组
      temp_array.splice(arrIndex, 1);
    } else {
      //数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项.
      break;
    }
  }
  return return_array;
}

六、合并数组对象中key相同的数据

合并之前数组 为 resData ,合并之后为 list_data 

其中 detail_date 为key值相同的key

 

 

let test_data = {};
let list_data = [];
resData.forEach((item, index) => {
      // console.log(item)
      let {detail_date} = item;
      if (!test_data[detail_date]) {
          test_data[detail_date] = {
              detail_date,
              listLiData: []
           }
       }
       test_data[detail_date].listLiData.push(item);

});
list_data = Object.values(test_data); // list 转换成功的数据

 七、数组排序

let a=[1,2,15,3,22,33]

  • 颠倒

//就是颠倒数组顺序
a.reverse(); //[33, 22, 3, 15, 2, 1]
  • 正序

//此方法不传入参数时,会使用默认排序方式。先调用每个元素的toString() 方法,然后按照转换后字符串的 Unicode 编码来进行排序
a.sort(); // [1, 15, 2, 22, 3, 33]

//若 a 小于 b,那么 a 会被排列到 b 之前。
//若 a 等于 b,a 和 b 的相对位置不变。
//若 a 大于 b,b 会被排列到 a 之前。
a.sort((a, b) => a - b); // [1, 2, 3, 15, 22, 33]
  • 倒序

a.sort((a, b) => b - a); //  [33, 22, 15, 3, 2, 1]

八、取值

let a=[1,2,3,4,5]

  • 数组在第一位追加数据
a.unshift(); //5,代表数组长度;
a.unshift(0); //返回6,数组长度;此时a=[0, 1, 2, 3, 4, 5];
  • 取出数组第一位
a.shift(); //1,不仅将数据取出,而且改变了数组,此时a=[2, 3, 4, 5]
  • 取出数组最后一位
a.pop(); //5,一样也是改变了数组,此时a=[1, 2, 3, 4]

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值