gpt4 book ai didi

javascript - 函数参数的问题我可以引用这个函数

转载 作者:行者123 更新时间:2023-12-02 21:10:09 24 4
gpt4 key购买 nike

let confirmed = {};
let deaths = {};
let recovered = {};
const objectWithParams = (param) =>
getCountryInfo.map((item) => {
let year, month, day;
const formatDate = ([year, month, day] = item.date
.split("-")
.map((element) => parseInt(element)));
for (var i = 0; i < getCountryInfo.length; i++) {
param = { x: new Date(formatDate), y: item.param };
}
return param;
});

confirmed = objectWithParams(`confirmed`);
console.log(confirmed);

如何在映射过程中引用函数参数

console.log 中的这段代码告诉我“y”未定义,我该如何做到这一点呢?

最佳答案

您不需要再次迭代。您已经在 map 中进行迭代。

示例:

let confirmed = {};
let deaths = {};
let recovered = {};
const objectWithParams = (param) =>
param.map(({ date, ...rest }) => {
const formatDate = date.split("-").map((element) => parseInt(element));
return { x: new Date(formatDate), ...rest };
});

confirmed = objectWithParams([
{
date: "03-04-2020",
death: 10,
active: 12,
recovered: 10,
},
{
date: "03-01-2020",
death: 10,
active: 12,
recovered: 10,
},
{
date: "03-02-2020",
death: 10,
active: 12,
recovered: 10,
},
{
date: "03-04-2020",
death: 10,
active: 12,
recovered: 10,
},
]);
console.log(confirmed);

映射器功能:

let confirmed = {};
let deaths = {};
let recovered = {};
class Mapper {
constructor(array, key) {
this.map = array.reduce((map, item) => {
const val = item[key];
if (!map[val]) {
map[val] = [];
}
map[val].push(item);
return map;
}, {});
}

find(key) {
return this.map[key] && this.map[key][Mapper.FIRST_INDEX]; //return blank array
}

findAll(key, returnUndefined) {
//return blank array
return this.map[key] ? this.map[key] : returnUndefined ? undefined : [];
}
}

Mapper.FIRST_INDEX = 0;
const data = [
{
date: "03-04-2020",
death: 10,
confirmed: 12,
recovered: 12,
},
{
date: "03-01-2020",
death: 10,
confirmed: 12,
recovered: 10,
},
{
date: "03-02-2020",
death: 10,
confirmed: 12,
recovered: 23,
},
{
date: "03-04-2020",
death: 10,
confirmed: 21,
recovered: 10,
},
]

var userMapper = new Mapper(data, "confirmed"); // create a map on names
console.log(JSON.stringify(userMapper.map, null, 4)); //will return only user with name 'Schmidt'
userMapper = new Mapper(data, "recovered"); // create a map on names
console.log(JSON.stringify(userMapper.map, null, 4)); //will return only user with name 'Schmidt'

关于javascript - 函数参数的问题我可以引用这个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61120497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com