Javascript 进行计算和筛选列表
Javascript make calculations and filter list
我有以下数组 -
const data=[
{
month:"nov",
veryLate:3,
Late:5,
onTime:2
},
{
month:"dec",
veryLate:1,
Late:3,
onTime:16
},
{
month:"jan",
veryLate:28,
Late:1,
onTime:1
},
}
我想对这个数组进行过滤和计算,以便获得百分比。
例如。 veryLate + Late+ onTime = (3+5+2) = 10
那么百分比是-
const data= [
{
month:"nov",
veryLate:30,
Late:50,
onTime:20
},
{
month:"dec",
veryLate:5,
Late:15,
onTime:80
},
,
{
month:"jan",
veryLate:98.33,
Late:3.33,
onTime:3.33
},]
为了计算这个我在下面执行了,但是在括号上出现语法错误 -
var filteredData=data.map(x=>{
x.month,
x.veryLate/(x.veryLate+x.Late+x.onTime)*100,
x.Late/(x.veryLate+x.Late+x.onTime)*100,
x.onTime/(x.veryLate+x.Late+x.onTime)*100,
});
如何获得计算结果?
map()
方法需要 return 一个值。
试试这个
var filteredData=data.map(x => {
// get the original values to avoid repeating x.
const {month, veryLate, Late, onTime} = x;
// do your calculations
const newVeryLate = veryLate / ( veryLate + Late + onTime) * 100;
const newLate = Late / (veryLate + Late + onTime) * 100;
const newOnTime = onTime / (veryLate + Late + onTime) * 100;
// return the new object
return {month, veryLate: newVeryLate, Late: newLate, onTime: newOnTime}
});
x.veryLate 不会在 x 中工作它应该是 veryLate 本身对于其他人来说是一样的
const data=[
{
month:"nov",
veryLate:3,
Late:5,
onTime:2
},
{
month:"dec",
veryLate:1,
Late:3,
onTime:16
},
{
month:"jan",
veryLate:28,
Late:1,
onTime:1
},
]
var filteredData= data.map(x => (
{
...x,
veryLate: x.veryLate/(x.veryLate+x.Late+x.onTime)*100,
Late: x.Late/(x.veryLate+x.Late+x.onTime)*100,
onTime: x.onTime/(x.veryLate+x.Late+x.onTime)*100
})
)
console.log(filteredData)
您还必须将返回的对象字面量括在括号中。目前花括号被表示为函数体。
var filteredData=data.reduce((a, v)=>{
let obj = {};
let sum = v.veryLate+v.Late+v.onTime;
obj.month = v.month;
obj.veryLate = v.veryLate/sum*100;
obj.Late = v.Late/sum*100;
obj.onTime = v.onTime/sum*100;
a.push(obj)
return a;
}, []);
抛出错误,因为:
- 数据数组没有右括号
- map 方法没有 returning 任何东西。如果你想 return 从中获取一个对象,请使用 return 关键字。
问题是传递给 Array.prototype.map 的回调需要为每次迭代 return 一些东西,而在您当前的实现中,您没有 returning 任何东西。
您可以使用map
如下图:
const data = [
{ month: "nov", veryLate: 3, Late: 5, onTime: 2 },
{ month: "dec", veryLate: 1, Late: 3, onTime: 16 },
{ month: "jan", veryLate: 28, Late: 1, onTime: 1 },
];
const filteredData = data.map(({ month, veryLate, Late, onTime }) => {
const total = veryLate + Late + onTime;
return {
month,
veryLate: (veryLate / total) * 100,
Late: (Late / total) * 100,
onTime: (onTime / total) * 100,
};
});
console.log(filteredData);
如果您发现 map
令人困惑,您也可以使用常规 for
循环来完成,如下所示:
const data = [
{ month: "nov", veryLate: 3, Late: 5, onTime: 2 },
{ month: "dec", veryLate: 1, Late: 3, onTime: 16 },
{ month: "jan", veryLate: 28, Late: 1, onTime: 1 },
];
const filteredData = [];
for (let i = 0; i < data.length; i++) {
const { month, veryLate, Late, onTime } = data[i];
const total = veryLate + Late + onTime;
result.push({
month,
veryLate: (veryLate / total) * 100,
Late: (Late / total) * 100,
onTime: (onTime / total) * 100,
});
}
console.log(filteredData);
其他文档:
我有以下数组 -
const data=[
{
month:"nov",
veryLate:3,
Late:5,
onTime:2
},
{
month:"dec",
veryLate:1,
Late:3,
onTime:16
},
{
month:"jan",
veryLate:28,
Late:1,
onTime:1
},
}
我想对这个数组进行过滤和计算,以便获得百分比。
例如。 veryLate + Late+ onTime = (3+5+2) = 10
那么百分比是-
const data= [
{
month:"nov",
veryLate:30,
Late:50,
onTime:20
},
{
month:"dec",
veryLate:5,
Late:15,
onTime:80
},
,
{
month:"jan",
veryLate:98.33,
Late:3.33,
onTime:3.33
},]
为了计算这个我在下面执行了,但是在括号上出现语法错误 -
var filteredData=data.map(x=>{
x.month,
x.veryLate/(x.veryLate+x.Late+x.onTime)*100,
x.Late/(x.veryLate+x.Late+x.onTime)*100,
x.onTime/(x.veryLate+x.Late+x.onTime)*100,
});
如何获得计算结果?
map()
方法需要 return 一个值。
试试这个
var filteredData=data.map(x => {
// get the original values to avoid repeating x.
const {month, veryLate, Late, onTime} = x;
// do your calculations
const newVeryLate = veryLate / ( veryLate + Late + onTime) * 100;
const newLate = Late / (veryLate + Late + onTime) * 100;
const newOnTime = onTime / (veryLate + Late + onTime) * 100;
// return the new object
return {month, veryLate: newVeryLate, Late: newLate, onTime: newOnTime}
});
x.veryLate 不会在 x 中工作它应该是 veryLate 本身对于其他人来说是一样的
const data=[
{
month:"nov",
veryLate:3,
Late:5,
onTime:2
},
{
month:"dec",
veryLate:1,
Late:3,
onTime:16
},
{
month:"jan",
veryLate:28,
Late:1,
onTime:1
},
]
var filteredData= data.map(x => (
{
...x,
veryLate: x.veryLate/(x.veryLate+x.Late+x.onTime)*100,
Late: x.Late/(x.veryLate+x.Late+x.onTime)*100,
onTime: x.onTime/(x.veryLate+x.Late+x.onTime)*100
})
)
console.log(filteredData)
您还必须将返回的对象字面量括在括号中。目前花括号被表示为函数体。
var filteredData=data.reduce((a, v)=>{
let obj = {};
let sum = v.veryLate+v.Late+v.onTime;
obj.month = v.month;
obj.veryLate = v.veryLate/sum*100;
obj.Late = v.Late/sum*100;
obj.onTime = v.onTime/sum*100;
a.push(obj)
return a;
}, []);
抛出错误,因为:
- 数据数组没有右括号
- map 方法没有 returning 任何东西。如果你想 return 从中获取一个对象,请使用 return 关键字。
问题是传递给 Array.prototype.map 的回调需要为每次迭代 return 一些东西,而在您当前的实现中,您没有 returning 任何东西。
您可以使用map
如下图:
const data = [
{ month: "nov", veryLate: 3, Late: 5, onTime: 2 },
{ month: "dec", veryLate: 1, Late: 3, onTime: 16 },
{ month: "jan", veryLate: 28, Late: 1, onTime: 1 },
];
const filteredData = data.map(({ month, veryLate, Late, onTime }) => {
const total = veryLate + Late + onTime;
return {
month,
veryLate: (veryLate / total) * 100,
Late: (Late / total) * 100,
onTime: (onTime / total) * 100,
};
});
console.log(filteredData);
如果您发现 map
令人困惑,您也可以使用常规 for
循环来完成,如下所示:
const data = [
{ month: "nov", veryLate: 3, Late: 5, onTime: 2 },
{ month: "dec", veryLate: 1, Late: 3, onTime: 16 },
{ month: "jan", veryLate: 28, Late: 1, onTime: 1 },
];
const filteredData = [];
for (let i = 0; i < data.length; i++) {
const { month, veryLate, Late, onTime } = data[i];
const total = veryLate + Late + onTime;
result.push({
month,
veryLate: (veryLate / total) * 100,
Late: (Late / total) * 100,
onTime: (onTime / total) * 100,
});
}
console.log(filteredData);
其他文档: