js取数组中对象的第一项(取对象的key和value)

 1. js取数组中对象的第一项

let types = res.data.result  //数组
console.log(types, "types")

     
let type = types.map((item) => {
          return item.type
 });

console.log(type, "type");

处理前:

 

处理后:

  2. js从数组中取出某个key的值相同的数据

 let types = res.data.result
 console.log(types, "types")


 var shebei = types.filter(function (obj) {
     return obj.type == 246;
  })
    console.log(shebei, "设备数组");
      this.setData({
         shebeiList: shebei
        })

处理前:

     

处理后:

    

ps:( 通过types中的246来获得对应246的其他值)

 let number = types.find(o => o.type == '246');
 console.log(number,"number");

 3. 拿一个对象的key和value:

let result = {
          1:0,
          2:0,
          3:0,
          4:0,
          5:6,
          6:4,
          7:7,
          8:1440,
          9:1000,
          10:1200,
          11:0,
          12:134
        }



Object.keys(result)

Object.values(result)

4.拿出key为0的value值 :

let newArr = Object.values(this.stsDateList)[0]

5. js过滤对象中的null值

var removePropertyOfNull=function(obj){
   Object.keys(obj).forEach(item=>{
        if(!obj[item])  delete obj[item]
       })
    return obj;
 }
 removePropertyOfNull(this.model)

6. 判断数组中某个字段是否为null

let ARR = this.List.filter(function(obj) {
     return obj.cvValue == null || obj.cvValue == ""; 
 })
if(ARR.length != 0){
  //有空值
}

  7. 数组遍历并组成新的数组

let arr = []; 
let arr1 = []; 
          
Array.forEach(i=>{
 if(i.key == "sch"){
  arr.push(i)
 }else if(i.key == "one"){ 
   arr1.push(i)
 }
})

 8. 两数组id相同的一项,将数组2的值赋给数组1

for (let i = 0; i < Array1.length; i++) {
   const Array1item = Array1[i];
   const Array2item = Array2.find(item => item.id === Array1item.id);
              
  if (Array2item) {
   Array1item.acA = Array2item.acA;
   Array1item.acO = Array2item.acO;
 }
}

9. 遍历数组中的每一项

res.result.forEach((item)=>{
    Object.keys(item).forEach(i=>{
          if(i != 'year' && i !='zsx'){
             item[i] = (item[i]/10000).toFixed(2)
       }
  })
})

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值