Skip to content

Instantly share code, notes, and snippets.

@joeyklee
Created February 21, 2019 22:29
Show Gist options
  • Save joeyklee/5604bb7d204ab1d2862e15f1fd12cdb6 to your computer and use it in GitHub Desktop.
Save joeyklee/5604bb7d204ab1d2862e15f1fd12cdb6 to your computer and use it in GitHub Desktop.
Nice use of filter and map to remove an item recursively found somewhere on stackoverflow
let myJson = {
"type": "list",
"name": "The P5.js Landscape",
"description": "This is a list of the P5.js landscape.",
"features": [
{
"type": "list",
"name": "Handy P5.js Tools",
"description": "This is a list of handy p5.js Tools ranging from commandline tools, project generators, and web editors.",
"features": [
{
"url": "https://p5js.org/",
"name": "p5js website",
"description": "p5js is a javascript library to make coding more accessible to everyone",
"clientId": "5gxQd02yqj"
},
{
"url": "https://www.npmjs.com/package/p5-manager",
"name": "P5 Manager commandline tool",
"description": "Commandline scaffolding tool for generating p5js projects",
"clientId": "moxtnVEDyl"
},
{
"url": "http://1023.io/p5-inspector/",
"name": "P5 playground",
"description": "A What you see is what you get editor for p5.js",
"clientId": "HOHy0C-B3h"
},
{
"url": "#",
"name": "New URL!",
"description": "A description for your new URL?",
"clientId": "no-MnmX-saQ"
}
],
"clientId": "zifyZ0V6mfv"
},
{
"type": "list",
"name": "P5 Libraries",
"description": "A list of libraries built for P5.js.",
"features": [
{
"url": "https://p5js.org/libraries/",
"name": "P5 Libraries ❤️️️❤️️️❤️️️",
"description": "officially on the website",
"clientId": "RBkPBiPJrJU"
},
{
"url": "https://github.com/generative-design/generative-design-library.js",
"name": "Generative Design Library🌈🌈🌈",
"description": "Generative Design library bundled with lots of other tools built for p5.js",
"clientId": "tJnqZDdXH2L"
}
],
"clientId": "Ob3cg_Jtu1e"
}
],
"clientId": "ggQlEMcHt0"
}
console.log( JSON.stringify(myJson) )
console.log( '\n\n\n\n' )
let newJson = Object.assign({}, myJson);
newJson = removeFromTree(newJson, 'zifyZ0V6mfv')
console.log( JSON.stringify(newJson) )
function removeFromTree(parent, featureid){
if(parent.clientId == featureid){
// delete parent
return {}
}
if(parent.features){
parent.features = parent.features
.filter(function(child){ return child.clientId !== featureid})
.map(function(child){ return removeFromTree(child, featureid)});
}
return parent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment