let datalist = [
{
'skuId': 1,
'skuName': 'test1',
'number': 11
},
{
'skuId': 2,
'skuName': 'test2',
'number': 22
},
{
'skuId': 3,
'skuName': 'test3',
'number': null
},
]
let price = datalist.reduce((sum, e) => sum + Number(e.number || 0), 0)
console.log(price) // 输出33
保留几位小数
obj.ratio = (obj.ratio*100).toFixed(0) + '%'
watch监视
sizeForm: {
name: "",
region: "",
radio: "",
}
watch: {
"sizeForm.radio": function () {
this.agreeChange();
this.sizeForm.radio == 1
? (this.table.td = this.tableGuest)
: (this.table.td = this.tableHost);
},
},
computed: {
"sizeForm.radio": function () {
this.agreeChange();
},
},
agreeChange(val) {
this.sitename = "";
let arr2 = [];
let arr3 = [];
this.table.data.forEach((item) => {
if (item.sitename.substring(0, 4) == "host") {
arr2.push(item);
} else {
arr3.push(item);
}
});
this.tableHost = arr2;
this.tableGuest = arr3;
if (val == 1) {
this.page.allCount = this.tableGuest.length;
} else {
this.page.allCount = this.tableHost.length;
}
},
js中数组转对象
var list = {};
var arr = ["123","456","789"];
for (var key in arr) {
list[key] = arr[key];
}
console.log(list);
将数组的字段取出组成key:value形式的对象
let list = [
{ key: 'php', value: '1' },
{ key: 'asp', value: '2' },
{ key: 'aspx', value: '3' },
{ key: 'jsp', value: '4' }
]
let header = {}
list.forEach(({ key, value }) => header[key] = value);
key:value 形式的对象组成数组
let list = []
let header = {
'php': '1',
'asp': '2',
'aspx': '3'
}
Object.keys(header).some((key) => {
list.push({ key: key, value: header[key] })
})
去除所有的id的key:value,保留其他的key:value
var newObj = {};
var arrList = [
{id:1, name:'惊鲵',sex:'1'},
{id:2, name:'掩日',sex:'2'},
{id:3, name:'黑白玄翦',sex:'3'}
];
var newArr = [];
arrList.forEach(function(item,i){
for ( var key in item) {
if(key != 'id'){
newObj[key] = item[key];
}
}
newArr.push(newObj);
newObj = {};//这步至关重要,每循环一次,都要清空一次,否则拿到的数据总是最后一条
});
console.log(newArr);
遍历数组元素,组成新对象
let thHeard=['feature', 'weight', 'fold', 'model']
let arr3 = []
let newArr = [
["x0", -0.12206059994258875, "fold_0", "model_0"],
["x1", -0.1171127243573215, "fold_0", "model_0"],
["x2", -0.11711, "fold_0", "model_0"],
["x0", -0.12206059994258875, "fold_0", "model_0"]
]
for(let i = 0; i < newArr.length; i++) {
let obj = {}
for(let j = 0; j < newArr.length; j++) {
console.log(newArr[i][j], 'newArr[i][j]');
obj[thHeard[j]] = newArr[i][j]
}
arr3.push(obj)
}
let county_list = [{
"id": 1,
"code": "110101",
"name": "东城区",
"citycode": "110100"
},
{
"id": 2,
"code": "110102",
"name": "西城区",
"citycode": "110100"
},
{
"id": 3,
"code": "110103",
"name": "崇文区",
"citycode": "110100"
},
]
let province_list = {}
county_list.forEach(({ code, name }) => province_list[code] = name);
数据处理
let arr1 = []
let list = [
{
create_time: "2022-07-28 14:29:10",
data_type: "ID",
field_description: "4",
field_name: "id"
}
]
list.forEach((item,index) => {
arr.push(
create_time = item.create_time,
data_type = item.data_type,
field_description= item.field_description,
field_name = item.field_name
)
})