1 回答
TA贡献1111条经验 获得超0个赞
您需要使用数组筛选方法。这是我用来检查是否已添加链接的示例。此外,它将更有效率,因为它将在数组中找到呈现的链接后跳过所有不必要的检查。once
links
let color = "darkred";
let source = "person1"; //this is generated elsewhere and changes
let target = "work23"; //this is generated elsewhere and changes
let link = {
color: color,
source: source,
target: target,
value: 1,
};
const links = [];
function person_linker(link) {
const linkAlreadyAdded = links.some(presentedLink => {
return (presentedLink.source === link.source) &&
(presentedLink.target === link.target)
});
if (linkAlreadyAdded) {
console.log('Check failed.');
} else {
console.log('Check passed.');
links.push(link);
}
}
console.log(links);
person_linker(link);
console.log(links);
person_linker(link);
console.log(links);
添加回答
举报