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);
添加回答
舉報