js取数组中对象的第一项(取对象的key和value)
let types = res.data.result//数组console.log(types, "types")let type = types.map((item) => {return item.type});console.log(type, "type");处理前:处理后:
·
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条内容
所有评论(0)