Hola, tenía un objeto Json como ese.
"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, } ] } ]Quiero eliminar el objeto json que contiene,
*** proveedor ** logotipo*0
Entonces quiero eliminar solo este objeto:
{ "type": "image-overlay", "path": "***vendor**logo*0", "zoomDirection": "in", "width": 0.7, }Mi código cortado así:
jsonObj.clips.map((clip, index)=>{ clip.layers.map((layer, index=>{ if(layer.path === '***vendor**logo*0'){ //Remove layer } }) })¿Cómo puedo hacer esto con nodejs? ¡Por favor ayuda!
Puede devolver todos los demás artículos excepto el que no desea
const json = /* your json array */ const newJson = json.map(node => node.layers.filter(layer => layer.path !== "***vendor**logo*0"));Tu pregunta no estaba muy clara, así que tuve que adivinar un poco. Pero aquí hay una solución potencial usando el escaneo de objetos
Actualmente, solo funciona con matrices, pero podría generalizarse fácilmente. Por favor hazme saber si tienes preguntas.
.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>Descargo de responsabilidad: soy el autor de object-scan