Created
August 13, 2025 22:41
-
-
Save unxok/4e3cbd28f804503e6e2cb038db34949f to your computer and use it in GitHub Desktop.
Obsidian Dataviewjs script to remove value from list-type properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Unxok | |
* @link https://github.com/unxok | |
* @license MIT | |
*/ | |
const parent = dv.container; | |
const settingsContainer = parent.createDiv(); | |
const btnContainer = parent.createDiv({ | |
attr: { style: "display: flex; justify-content: end; gap: 5px;" }, | |
}); | |
let listPropertyName = ""; | |
let valueToRemove = ""; | |
const updatePropertyInNotes = async ({ listPropertyName, valueToRemove }) => { | |
let updatedCount = 0; | |
const files = app.vault.getMarkdownFiles(); | |
await Promise.all( | |
files.map(async (file) => { | |
const metadata = app.metadataCache.getCache(file.path); | |
const listPropertyValue = metadata?.frontmatter?.[listPropertyName]; | |
if (!listPropertyValue || !Array.isArray(listPropertyValue)) return; | |
const newValue = listPropertyValue.filter((v) => v !== valueToRemove); | |
await app.fileManager.processFrontMatter(file, (fm) => { | |
fm[listPropertyName] = newValue; | |
}); | |
updatedCount++; | |
}) | |
); | |
new Notice(`Updated ${updatedCount} notes`); | |
}; | |
const createSetting = ({ parent, title, desc, addControl }) => { | |
const itemEl = parent.createDiv({ cls: "setting-item" }); | |
const infoEl = itemEl.createDiv({ cls: "setting-item-info" }); | |
infoEl.createDiv({ cls: "setting-item-name", text: title }); | |
infoEl.createDiv({ cls: "setting-item-description", text: desc }); | |
const controlEl = itemEl.createDiv({ cls: "setting-item-control" }); | |
addControl(controlEl); | |
}; | |
const renderSettings = (parent) => { | |
createSetting({ | |
parent, | |
title: "Property name", | |
desc: "The list-type property that will be updated", | |
addControl: (el) => { | |
const input = el.createEl("input", { | |
attr: { | |
type: "text", | |
spellcheck: "false", | |
}, | |
}); | |
input.addEventListener("input", (e) => { | |
listPropertyName = e.target.value; | |
}); | |
}, | |
}); | |
createSetting({ | |
parent, | |
title: "List value", | |
desc: "The single item value to remove from the list property", | |
addControl: (el) => { | |
const input = el.createEl("input", { | |
attr: { | |
type: "text", | |
spellcheck: "false", | |
}, | |
}); | |
input.addEventListener("input", (e) => { | |
valueToRemove = e.target.value; | |
}); | |
}, | |
}); | |
}; | |
btnContainer | |
.createEl("button", { text: "reset" }) | |
.addEventListener("click", () => { | |
listPropertyName = ""; | |
valueToRemove = ""; | |
renderSettings(parent); | |
}); | |
btnContainer | |
.createEl("button", { text: "update notes", cls: "mod-cta" }) | |
.addEventListener("click", async () => { | |
if (listPropertyName === "") { | |
new Notice("Property name must not be empty!"); | |
return; | |
} | |
if (valueToRemove === "") { | |
new Notice("List value must not be empty!"); | |
return; | |
} | |
await updatePropertyInNotes({ listPropertyName, valueToRemove }); | |
}); | |
renderSettings(settingsContainer); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2025 Unxok | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment