- 提取数组中指定参数,并返回该参数的集合
应用场景:在一个数组对象中想要直接获取某个参数值的集合
getFiledArr(arrs, paramsname) {
let arr = [];
arr = arrs.map(d => d[paramsname]) || [];
return arr;
}
const datas = [
{
"ItemName": "账面原值(万元)",
"ItemValue": "901.00",
"ItemOldValue": "903.00",
},
{
"ItemName": "土地账面原值(万元)",
"ItemValue": "991.00",
"ItemOldValue": "905.00",
}
];
console.log("ItemName集合:", getFiledArr(datas, 'ItemName'));
console.log("ItemValue集合:", getFiledArr(datas, 'ItemValue'));
/**
* 求和
* @param {*} arr 数组
* @param {*} params 求和参数
* @memberof PsurveyContainerComponent
*/
computeTotal(arr, params) {
let total = 0;
total = arr.reduce((acc, cur) => {
return acc + (cur[params] || 0)
}, 0)
return total
};
const datas = [
{ name: "数学", fraction: 130 },
{ name: "语文", fraction: 140 },
{ name: "英语", fraction: 125 },
{ name: "理综", fraction: 280 }
]
console.log("总成绩:", computeTotal(datas, 'fraction') )
- 提取指定数组中,指定的两个参数组合,并返回字符串
应用场景:特定场合使用
function getFiledValueStr(arr, filedname, filedvalue) {
let str = "";
arr.map((d,i) => {
const strname = d[filedname] + ":" + d[filedvalue];
if (i !== 0) str += ("," + strname);
else str += strname;
})
return str
}
const datas = [
{
"ItemName": "账面原值(万元)",
"ItemValue": "901.00",
"ItemOldValue": "903.00",
},
{
"ItemName": "土地账面原值(万元)",
"ItemValue": "991.00",
"ItemOldValue": "905.00",
}
];
console.log("变更前:", getFiledValueStr(datas, 'ItemName', 'ItemOldValue'));
console.log("变更后:", getFiledValueStr(datas, 'ItemName', 'ItemValue'));