gpt4 book ai didi

javascript - 如何在处理单词标记时解决 ƒ Object() { [native code] } 错误?

转载 作者:行者123 更新时间:2023-11-30 11:12:30 26 4
gpt4 key购买 nike

考虑一个包含 N 个项目的数组。

data = [
{"source":"post","target":"incident"},
{"source":"incident","target":"analysis"},
{"source":"october","target":"post"},
{"source":"mef","target":"composing"},
{"source":"trend","target":"archive"},
{"source":"object","target":"constructor"},
{"source":"constructor","target":"parameter"}
]

我有一个函数可以根据这些数据创建一个节点矩阵。比方说:

[[0, 1, 0, 1, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0]]

此函数适用于给定数据。但是,我注意到在这个特定的 sourcestargets 中它返回了一个错误。问题在于其中的单词:constructor。例如,如果我将 constructor 更改为 constructr,函数将返回矩阵。

我发现这部分有问题:

let nodes = {}
data.map(link => {
nodes[link.source] || (nodes[link.source] = 1)
nodes[link.target] || (nodes[link.target] = 1)
})
nodes = d3.keys(nodes)
nodes.sort()

在此过程之后,nodes 仅包含以下数据:["analysis", "archive", "composing", "incident", "mef", "object", "october"", "parameter", "post", "trend"] 但缺少构造函数。

当我尝试控制某些数据时:此特定节点返回此:ƒ Object() { [native code] }

我解决了这个问题,将每个单词转换为大写 toUpperCase()。但是,我想知道是否有其他方法可以解决这个问题。

提前致谢。

最佳答案

javascript 中的大多数对象都有构造方法 - 参见 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

感谢 user2864740 的评论 - 逻辑上接近原始的代码 - 使用 .hasOwnProperty - 由于 constructor 是继承的,这将是错误的,直到你显式赋值

data = [
{"source":"post","target":"incident"},
{"source":"incident","target":"analysis"},
{"source":"october","target":"post"},
{"source":"mef","target":"composing"},
{"source":"trend","target":"archive"},
{"source":"object","target":"constructor"},
{"source":"constructor","target":"parameter"}
]
let nodes = {};

data.forEach(link => {
nodes.hasOwnProperty(link.source) || (nodes[link.source] = 1);
nodes.hasOwnProperty(link.target) || (nodes[link.target] = 1);
})
nodes = Object.keys(nodes)
nodes.sort()
console.log(nodes.join());

或者,您可以使用 Object.create(null);

请阅读 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Custom_and_Null_objects 中使用 Object.create(null) 时的注意事项

只要您知道这些限制,就不会有任何问题

data = [
{"source":"post","target":"incident"},
{"source":"incident","target":"analysis"},
{"source":"october","target":"post"},
{"source":"mef","target":"composing"},
{"source":"trend","target":"archive"},
{"source":"object","target":"constructor"},
{"source":"constructor","target":"parameter"}
]
let nodes = Object.create(null);

data.forEach(link => {
nodes[link.source] || (nodes[link.source] = 1)
nodes[link.target] || (nodes[link.target] = 1)
})
nodes = Object.keys(nodes)
nodes.sort()
console.log(nodes.join());

当然,您的代码失败的原因是因为节点已经具有 constructor 属性(以及许多其他属性)

所以

nodes[link.target] || (nodes[link.target] = 1)

nodes.constructor || (nodes.constructor = 1)

并且因为 nodes.constructor 是 truthy 它永远不会被值 1“覆盖”

你可以简单地做

  nodes[link.source] = 1;
nodes[link.target] = 1;

结果相同,实际上没有必要这样做:

  nodes[link.source] || (nodes[link.source] = 1);
nodes[link.target] || (nodes[link.target] = 1);

请看下面的片段:

data = [
{"source":"post","target":"incident"},
{"source":"incident","target":"analysis"},
{"source":"october","target":"post"},
{"source":"mef","target":"composing"},
{"source":"trend","target":"archive"},
{"source":"object","target":"constructor"},
{"source":"constructor","target":"parameter"}
]
let nodes = {};

data.forEach(link => {
nodes[link.source] = 1;
nodes[link.target] = 1;
})
nodes = Object.keys(nodes)
nodes.sort()
console.log(nodes.join());

关于javascript - 如何在处理单词标记时解决 ƒ Object() { [native code] } 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53095056/

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