简体   繁体   中英

Remove parent JSON element depending on child value

I have a JSON with lots of empty content:

{
    "items": [
        {
            "category": "login",
            "fields": [
                {
                    "label": "Name",
                    "value": "",
                },
                {
                    "label": "E-Mail",
                    "value": "",
                },
                {
                    "label": "Password",
                    "value": "123456",
                },
                {
                    "label": "Website",
                    "fields": [
                        {
                            "label": "Name X",
                            "value": ""
                        },
                        {
                            "label": "Name Y",
                            "value": "another one"
                        },…
                    ]
                },…
            ]
        },…
    ]
}

The nesting goes several levels deeper. This shows only the first level. I want to delete all elements of "fields" (or whatever the array's key in deeper levels is), where their "value" is empty.

{
    "items": [
        {
            "category": "login",
            "fields": [
                {
                    "label": "Password",
                    "value": "123456",
                },
                {
                    "label": "Website",
                    "fields": [
                        {
                            "label": "Name Y",
                            "value": "another one"
                        },…
                    ]
                },…
            ]
        },…
    ]
}

How can I do this in Javascript?

Well, I found a way to iterate through the JSON object:

function remove(jsondata) {
  for (let i in jsondata) {
    if (jsondata[i].value != undefined && jsondata[i].value == '') {
       delete jsondata[i];
    }
    else if (typeof jsondata[i] === "object") remove(jsondata[i]);
  }
}

Not sure, if it's the most elegant way, but it works so far.

use filter method,you could get a filtered array
it returned Boolean . if value exist,it will be true

var list=JSON.parse(data)
list.items=list.items.map(val=>{
  val.fields=val.fields.filter(v=>v.value})
  return val
})

We use object-scan for many data processing tasks. It's powerful once you wrap your head around it. Here is how you could answer your questions

 // const objectScan = require('object-scan'); const prune = (input) => objectScan(['**[*].value'], { rtn: 'count', filterFn: ({ gparent, gproperty, value }) => { if (value === '') { gparent.splice(gproperty, 1); return true; } return false; } })(input); const obj = { items: [{ category: 'login', fields: [{ label: 'Name', value: '' }, { label: 'E-Mail', value: '' }, { label: 'Password', value: '123456' }, { label: 'Website', fields: [{ label: 'Name X', value: '' }, { label: 'Name Y', value: 'another one' }] }] }] }; console.log(prune(obj)); // return count of pruned entries // => 3 console.log(obj); // => { items: [ { category: 'login', fields: [ { label: 'Password', value: '123456' }, { label: 'Website', fields: [ { label: 'Name Y', value: 'another one' } ] } ] } ] }
 .as-console-wrapper {max-height: 100%;important: top: 0}
 <script src="https://bundle.run/object-scan@16.0.0"></script>

Disclaimer : I'm the author of object-scan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM