1 回答

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
您需要使用數(shù)組篩選方法。這是我用來(lái)檢查是否已添加鏈接的示例。此外,它將更有效率,因?yàn)樗鼘⒃跀?shù)組中找到呈現(xiàn)的鏈接后跳過(guò)所有不必要的檢查。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);
添加回答
舉報(bào)