Hi I had an Json Object like that
"clips"[
{
"layers": [
{
"type": "image-overlay",
"path": "http://google.com",
},
{
"type": "slide-in-text",
"text": "Some Bags"
},
]
},
{
"duration": 3,
"layers": [
{
"type": "image",
"path": "http://google.com",
"resizeMode": "stretch",
"start": 0,
"stop": 3,
},
{
"type": "image-overlay",
"path": "***vendor**logo*0",
"zoomDirection": "in",
"width": 0.7,
}
]
}
]
I want to remove json object which contain,
*** vendor ** logo*0
So I want to remove only this object:
{
"type": "image-overlay",
"path": "***vendor**logo*0",
"zoomDirection": "in",
"width": 0.7,
}
My code snipped like that:
jsonObj.clips.map((clip, index)=>{
clip.layers.map((layer, index=>{
if(layer.path === '***vendor**logo*0'){
//Remove layer
}
})
})
How Can I do this with nodejs? Please Help!
You could return all other items except the one you don't want
const json = /* your json array */
const newJson = json.map(node => node.layers.filter(layer => layer.path !== "***vendor**logo*0"));
Your question wasn't very clear, so I had to guess a bit. But here is a potential solution using object-scan
Currently, it only works with arrays, but it could easily be generalized. Please let me know if you have any questions.
.as-console-wrapper {max-height: 100% !important; top: 0}
<script type="module">
import objectScan from 'https://cdn.jsdelivr.net/npm/object-scan@18.1.2/lib/index.min.js';
const input = { clips: [{ layers: [{ type: 'image-overlay', path: 'http://google.com' }, { type: 'slide-in-text', text: 'Some Bags' }] }, { duration: 3, layers: [{ type: 'image', path: 'http://google.com', resizeMode: 'stretch', start: 0, stop: 3 }, { type: 'image-overlay', path: '***vendor**logo*0', zoomDirection: 'in', width: 0.7 }] }] };
const rm = (obj, v) => objectScan(['**[*].*'], {
abort: true,
rtn: 'bool',
filterFn: ({ gparent, gproperty, value }) => {
if (value === v) {
gparent.splice(gproperty, 1);
return true;
}
return false;
}
})(obj);
console.log(rm(input, '***vendor**logo*0'));
// => true
console.log(input);
// => { clips: [ { layers: [ { type: 'image-overlay', path: 'http://google.com' }, { type: 'slide-in-text', text: 'Some Bags' } ] }, { duration: 3, layers: [ { type: 'image', path: 'http://google.com', resizeMode: 'stretch', start: 0, stop: 3 } ] } ] }
</script>
Disclaimer: I'm the author of object-scan