我试图在以下数组中返回破坏关系的所有项目:
[
{ id: "1", option: { bound_id: "2" }},
{ id: "2", option: { bound_id: "12" }},
{ id: "12", option: { bound_id: "2" }}
]
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,每个项目都使用该属性相互链接bound_id,如果属性破坏了以下关系:
[
{ id: "1", option: { bound_id: null }},
{ id: "2", option: { bound_id: "12" }},
{ id: "12", option: { bound_id: "2" }}
]
Run Code Online (Sandbox Code Playgroud)
返回以下结果:
[
{ id: "2", option: { bound_id: "12" }}
{ id: "12", option: { bound_id: "2" }}
]
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码:
const input = [ …Run Code Online (Sandbox Code Playgroud)我有一个对象列表,其中包含一个名为的属性created:
let objs = [
{ id: "1", created: "2019-02-12 00:00:00" },
{ id: "2", created: "2018-02-12 00:00:00" },
{ id: "3", created: "2017-02-12 00:00:00" },
{ id: "4", created: "2016-02-12 00:00:00" },
{ id: "5", created: "2015-02-12 00:00:00" }
]
Run Code Online (Sandbox Code Playgroud)
我想要返回所有2019年和月份的记录:02,所以我做了:
var curr_date = moment(moment(), "YYYY-MM-DD");
var in_month = objs.filter((item) => function () {
return (moment(item.created, format).month() == curr_date.month() &&
moment(item.created, format).year() == curr_date.year())
});
Run Code Online (Sandbox Code Playgroud)
问题是in_month总是返回整个数组,为什么?预期输出应为id为1的记录
我刚刚开始学习NodeJS,并c#在尝试async / await操作员的知识的推动下,无论如何,我需要问我如何等待获得结果的承诺,特别是:
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(user.password, salt);
Run Code Online (Sandbox Code Playgroud)
上面的代码hash使用user密码和盐生成一个。这就像一种魅力,但是假设我要按以下方式破坏代码:
const hash = await bcrypt.genSalt(10, async (err, salt) => {
return await bcrypt.hash(user.password, salt);
});
Run Code Online (Sandbox Code Playgroud)
我会明白undefined,我做错了什么?
第一个或第二个版本更好吗?只是以这种方式开始我。
提前致谢。