简体   繁体   中英

Stop Deeply Nested JSON Array Items/Objects Based on Element Name

So, I'm relatively new to javascript, and I'm working with a client-side javascript function that inserts JSON results pushed to it from the server.

I would like to stop certain results, independently of that javascript that populating the results.

Usually, if the js was my own, I'd make a guess that I'd use the "splice" function to remove the JSON array entry(ies) that I don't wish to be there, does that sound about right? (I'd got to there through plenty of searching on StartPage and here)

However, I need whatever function I (let's be honest, "you") come up with to trump the embedded results.

Essentially this is normal behaviour: Webpage > JavaScript Parses JSON for Initial Results then Webpage > Scroll > JavaScript Parses JSON for More Results

I'd like to insert something that will stop any JSON items that I don't like. I don't know how to represent that, I'd assume: Worker in Extension/Add-On > Webpage > etc I doubt this can be done with CSS... but that'd be awesome, as I'd just put it in my personal.

If it was splice, I'd have a vague idea (but really I'm flying blind) of how to do it on a base level with splice, but I'm already struggling, and that would deepen for the insanely deeply embedded objects I'm trying to exclude.


So. There is lot of json data in the section I'm looking at.

I'm purely concentrated on the removing of the array here, but by end goal is two parts:

  1. In the ' contents ' array in levelOneB 's ' daContentShaper ' I would like to remove any item from the array where in the sub-object ' content ' there is an element called ' contentTypeA '. There's another object with the label ' ahThisIsTheBContent ', which I'd also like gone, and that's dotted through the JSON. All other items (TypeB, TypeC, or other) must stay.
  2. Additionally, within said ' content ' object I would like to change any ' visible ' values that are " false " to " true ".

It's definitely possible, it's just working out how to ensure that the rest of the code (which may have similar - not the same - parts elsewhere) isn't affected by my splice of this array.

{
    "levelOneA": {
        "levelOneAArr": [ { "name": "me", "age": 1 }, { "name": "you", "age": 2 }, { "name": "them", "age": 3 } ],
        "levelOneAText": "blah"
    },
    "levelOneB": {
        "levelOneBDeepBro": {
            "levelOneBDeepBroTabs": [
                {
                    "levelOneBDeepBroTabsShaper": {
                        "isBlessed": true,
                        "daContent": {
                            "daContentShaper": {
                                "contents": [
                                    {
                                        "contentItem": {
                                            "content": {
                                                "contentTypeA": {
                                                    "bitA": "bitAText",
                                                    "bitB": [
                                                        {
                                                            "bitBText1": "cripes"
                                                        },
                                                        {
                                                            "bitBText1": "crikey"
                                                        },
                                                        {
                                                            "bitBText1": "crimeny"
                                                        }
                                                    ],
                                                    "visible": true
                                                }
                                            },
                                            "extraNonsense": "bingoBlah"
                                        }
                                    },
                                    {
                                        "contentItem": {
                                            "content": {
                                                "ahThisIsTheBContent": {
                                                    "bitA": "bitAText",
                                                    "bitB": [
                                                        {
                                                            "bitBText1": "cripes"
                                                        },
                                                        {
                                                            "bitBText1": "crikey"
                                                        },
                                                        {
                                                            "bitBText1": "crimeny"
                                                        }
                                                    ],
                                                    "visible": false
                                                }
                                            },
                                            "extraNonsense": "bingoBlahBlah"
                                        }
                                    },
                                    {
                                        "contentItem": {
                                            "content": {
                                                "contentTypeB": {
                                                    "bitA": "bitAText",
                                                    "bitB": [
                                                        {
                                                            "bitBText1": "jinkies"
                                                        },
                                                        {
                                                            "bitBText1": "ruh roh"
                                                        },
                                                        {
                                                            "bitBText1": "like, wow"
                                                        }
                                                    ],
                                                    "visible": true
                                                }
                                            },
                                            "extraNonsense": "bingoBlahBlah"
                                        }
                                    },
                                    {
                                        "contentItem": {
                                            "content": {
                                                "contentTypeC": {
                                                    "bitA": "bitAText",
                                                    "bitB": [
                                                        {
                                                            "bitBText1": "cripes"
                                                        },
                                                        {
                                                            "bitBText1": "crikey"
                                                        },
                                                        {
                                                            "bitBText1": "crimeny"
                                                        }
                                                    ],
                                                    "visible": true
                                                }
                                            },
                                            "extraNonsense": "bingoBlahBlahBlah"
                                        }
                                    }
                                ],
                                "myDadIsBetter": "Go Faster Stripes, innit."
                            }
                        },
                        "tabId": "Bro"
                    }
                }
            ]
        }
    },
    "levelOneC": "CAAQhGciEwi0iqD5-ZbwAhUBPvEFHQMKAeo=",
    "levelOneD": {
        "iAmJustHappyToBeNominated": "innit",
        "damnYourWholePopCultureSubsection": "flames abound"
    }
}

This is how you could solve this using object-scan

Note that I used quite a few ** in the matching. You could write these out exactly, it depends on your requirement

 // const objectScan = require('object-scan'); const data = { levelOneA: { levelOneAArr: [{ name: 'me', age: 1 }, { name: 'you', age: 2 }, { name: 'them', age: 3 }], levelOneAText: 'blah' }, levelOneB: { levelOneBDeepBro: { levelOneBDeepBroTabs: [ { levelOneBDeepBroTabsShaper: { isBlessed: true, daContent: { daContentShaper: { contents: [ { contentItem: { content: { contentTypeA: { bitA: 'bitAText', bitB: [ { bitBText1: 'cripes' }, { bitBText1: 'crikey' }, { bitBText1: 'crimeny' } ], visible: true } }, extraNonsense: 'bingoBlah' } }, { contentItem: { content: { ahThisIsTheBContent: { bitA: 'bitAText', bitB: [ { bitBText1: 'cripes' }, { bitBText1: 'crikey' }, { bitBText1: 'crimeny' } ], visible: false } }, extraNonsense: 'bingoBlahBlah' } }, { contentItem: { content: { contentTypeB: { bitA: 'bitAText', bitB: [ { bitBText1: 'jinkies' }, { bitBText1: 'ruh roh' }, { bitBText1: 'like, wow' } ], visible: false } }, extraNonsense: 'bingoBlahBlah' } }, { contentItem: { content: { contentTypeC: { bitA: 'bitAText', bitB: [ { bitBText1: 'cripes' }, { bitBText1: 'crikey' }, { bitBText1: 'crimeny' } ], visible: false } }, extraNonsense: 'bingoBlahBlahBlah' } } ], myDadIsBetter: 'Go Faster Stripes, innit.' } }, tabId: 'Bro' } } ] } }, levelOneC: 'CAAQhGciEwi0iqD5-ZbwAhUBPvEFHQMKAeo=', levelOneD: { iAmJustHappyToBeNominated: 'innit', damnYourWholePopCultureSubsection: 'flames abound' } }; const logic = { 'levelOneB.**.daContentShaper.contents[*].*.content.{contentTypeA,ahThisIsTheBContent}': ({ key, parents }) => { parents[3].splice(key[key.length - 4], 1); }, 'levelOneB.**.daContentShaper.contents.**.visible': ({ value, parent, property }) => { if (value === false) { parent[property] = true; } } }; const modify = objectScan(Object.keys(logic), { rtn: 'count', filterFn: ({ matchedBy, property, key, value, parent, parents }) => { matchedBy.forEach((m) => logic[m]({ property, key, value, parent, parents })); } }); console.log(modify(data)); // => 6 console.log(data); // => { levelOneA: { levelOneAArr: [ { name: 'me', age: 1 }, { name: 'you', age: 2 }, { name: 'them', age: 3 } ], levelOneAText: 'blah' }, levelOneB: { levelOneBDeepBro: { levelOneBDeepBroTabs: [ { levelOneBDeepBroTabsShaper: { isBlessed: true, daContent: { daContentShaper: { contents: [ { contentItem: { content: { contentTypeB: { bitA: 'bitAText', bitB: [ { bitBText1: 'jinkies' }, { bitBText1: 'ruh roh' }, { bitBText1: 'like, wow' } ], visible: true } }, extraNonsense: 'bingoBlahBlah' } }, { contentItem: { content: { contentTypeC: { bitA: 'bitAText', bitB: [ { bitBText1: 'cripes' }, { bitBText1: 'crikey' }, { bitBText1: 'crimeny' } ], visible: true } }, extraNonsense: 'bingoBlahBlahBlah' } } ], myDadIsBetter: 'Go Faster Stripes, innit.' } }, tabId: 'Bro' } } ] } }, levelOneC: 'CAAQhGciEwi0iqD5-ZbwAhUBPvEFHQMKAeo=', levelOneD: { iAmJustHappyToBeNominated: 'innit', damnYourWholePopCultureSubsection: 'flames abound' } }
 .as-console-wrapper {max-height: 100%;important: top: 0}
 <script src="https://bundle.run/object-scan@14.3.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