为数据集中发生的每种类型的事件计算相关性
computing a correlation for every type of event that occurs in the data set
我重写了 eloquent javascript 代码,将其分解为我自己的理解,与书中的简写形式不同。在 运行 代码之后,而不是我的事件被放入我的空 "let events = 0;" 数组并重新检查“!events.includes(事件)”然后执行 "events.push(event)"。我希望事件被推送到我的空数组,但 JOURNAL 数据中的事件数组被推送到我的空事件中,留下所有事件,而不是仅包含每个事件列表的一个数组。
function journalEvents(journal) {
let events = [];
for (i = 0; i < journal.length; i++) {
let entry = journal[i];
for (j = 0; j < journal.length; j++) {
let event = entry.events;
if (!events.includes(event)) {
events.push(event);
}
}
}
return events;
}
console.log(journalEvents(JOURNAL));
// Why am I getting an array in and array instead → [["carrot", "exercise", "weekend"], …]
Bellow 是完美运行的 Eloquent 代码,我无法从速记中找到我自己重写的代码之间的区别。有人能告诉我为什么我不整理所有事件而不是将 JOURNAL[i].event 推入我的数组吗?....
function journalEvents(journal) {
let events = [];
for (let entry of journal) {
for (let event of entry.events) {
if (!events.includes(event)) {
events.push(event);
}
}
}
return events;
}
console.log(journalEvents(JOURNAL));
// This is the ectaul expected result (A single array of events) → ["carrot", "exercise", "weekend", "bread", …]
// can someone please break this code down for me line by line?
function journalEvents(journal) {
/* how come this block could use the same name "events" as it is in the
JOURNAL data to set its output collections of arrays and does not affect the
code "JOURNAL[i].events" in the date when it loop through?.
Isn't that variable declaration at the begining of the loop sets the events
in the JOURNAL data to an empty array everytime it runs?*/
let events = [];
for (let entry of journal) {
for (let event of entry.events) {
if (!events.includes(event)) {
/* Why are the events pushed in individually and not in their arrays? */
events.push(event);
}
}
}
return events;
}
console.log(journalEvents(JOURNAL));
// → ["carrot", "exercise", "weekend", "bread", …]
您的两个脚本之间的区别在于循环中使用了 of
关键字,这意味着 遍历值并忽略键 .
在你的循环中,你使用 j
作为键的指示符,但你不是基于键关联推送,而是推送 event
的整个值,你应该按以下方式推送 event[j]
:
if (!events.includes(event[j])) {
events.push(event[j]);
}
这是基于原始 JOURNAL
数据集值的假设,您的示例中未提供该值,但是您在代码段底部的评论中有足够的内容可以得出问题所在,其中 JOURNAL
可能类似于以下内容(纯粹是有根据的猜测):
[
["carrot", "exercise", "weekend"],
["carrot", "exercise", "weekend"],
// ... etc
]
of
运算符是 ES6 specification 的一部分,它被包括在内是为了用不包括原型值的 for ... in
代替常见的传统笨拙的迭代方式,传统上看起来像下面这样:
for (i in object)
{
if (object.hasOwnPrototype(i))
{
var.push(i);
}
}
这显然有些笨拙,因为 for ... in
遍历 所有键,包括原型链 (最常见的情况是不想迭代原型链) , 并且 foreach
传统上性能不佳,因此添加了 for ... of
循环。如果你用 for (i=0; i < some_array.length; i++)
来模拟它,那么你需要用键 i
显式引用值,比如 object[i]
,它也只适用于数组,而不适用于与字符串关联键控的对象. for ... of
适用于两者,以及任何其他类似数组的对象,使其更加通用并从整体上降低代码的复杂性和冗余。
我重写了 eloquent javascript 代码,将其分解为我自己的理解,与书中的简写形式不同。在 运行 代码之后,而不是我的事件被放入我的空 "let events = 0;" 数组并重新检查“!events.includes(事件)”然后执行 "events.push(event)"。我希望事件被推送到我的空数组,但 JOURNAL 数据中的事件数组被推送到我的空事件中,留下所有事件,而不是仅包含每个事件列表的一个数组。
function journalEvents(journal) {
let events = [];
for (i = 0; i < journal.length; i++) {
let entry = journal[i];
for (j = 0; j < journal.length; j++) {
let event = entry.events;
if (!events.includes(event)) {
events.push(event);
}
}
}
return events;
}
console.log(journalEvents(JOURNAL));
// Why am I getting an array in and array instead → [["carrot", "exercise", "weekend"], …]
Bellow 是完美运行的 Eloquent 代码,我无法从速记中找到我自己重写的代码之间的区别。有人能告诉我为什么我不整理所有事件而不是将 JOURNAL[i].event 推入我的数组吗?....
function journalEvents(journal) {
let events = [];
for (let entry of journal) {
for (let event of entry.events) {
if (!events.includes(event)) {
events.push(event);
}
}
}
return events;
}
console.log(journalEvents(JOURNAL));
// This is the ectaul expected result (A single array of events) → ["carrot", "exercise", "weekend", "bread", …]
// can someone please break this code down for me line by line?
function journalEvents(journal) {
/* how come this block could use the same name "events" as it is in the
JOURNAL data to set its output collections of arrays and does not affect the
code "JOURNAL[i].events" in the date when it loop through?.
Isn't that variable declaration at the begining of the loop sets the events
in the JOURNAL data to an empty array everytime it runs?*/
let events = [];
for (let entry of journal) {
for (let event of entry.events) {
if (!events.includes(event)) {
/* Why are the events pushed in individually and not in their arrays? */
events.push(event);
}
}
}
return events;
}
console.log(journalEvents(JOURNAL));
// → ["carrot", "exercise", "weekend", "bread", …]
您的两个脚本之间的区别在于循环中使用了 of
关键字,这意味着 遍历值并忽略键 .
在你的循环中,你使用 j
作为键的指示符,但你不是基于键关联推送,而是推送 event
的整个值,你应该按以下方式推送 event[j]
:
if (!events.includes(event[j])) {
events.push(event[j]);
}
这是基于原始 JOURNAL
数据集值的假设,您的示例中未提供该值,但是您在代码段底部的评论中有足够的内容可以得出问题所在,其中 JOURNAL
可能类似于以下内容(纯粹是有根据的猜测):
[
["carrot", "exercise", "weekend"],
["carrot", "exercise", "weekend"],
// ... etc
]
of
运算符是 ES6 specification 的一部分,它被包括在内是为了用不包括原型值的 for ... in
代替常见的传统笨拙的迭代方式,传统上看起来像下面这样:
for (i in object)
{
if (object.hasOwnPrototype(i))
{
var.push(i);
}
}
这显然有些笨拙,因为 for ... in
遍历 所有键,包括原型链 (最常见的情况是不想迭代原型链) , 并且 foreach
传统上性能不佳,因此添加了 for ... of
循环。如果你用 for (i=0; i < some_array.length; i++)
来模拟它,那么你需要用键 i
显式引用值,比如 object[i]
,它也只适用于数组,而不适用于与字符串关联键控的对象. for ... of
适用于两者,以及任何其他类似数组的对象,使其更加通用并从整体上降低代码的复杂性和冗余。