Javascript/JSON Object Array Search Issue

Viewed 80

I am trying to search results (that I get back from a SOAP API call) which are what I believe to be a Javascript object array since it doesn't follow JSON standards.

Here is a sample of the data:

[ { '$': { uuid: '{461F892D-07D8-4FCA-AF86-D5A9CBB080B8}' },
    pattern: [ '*689XXXX' ],
    description: [ 'DHHS_62' ],
    routePartitionName: [ 'DHHS_62-PT' ] },
  { '$': { uuid: '{84651C57-5E4C-4981-B859-38C8ECC56E3B}' },
    pattern: [ '*745XXXX' ],
    description: [ 'DHHS_43' ],
    routePartitionName: [ 'DHHS_43-PT' ] },
  { '$': { uuid: '{BC3259C4-38DB-4AD6-8594-61B938E4542C}' },
    pattern: [ '*753XXXX' ],
    description: [ 'DEQ_50' ],
    routePartitionName: [ 'DEQ_50-PT' ] },
  { '$': { uuid: '{4CF32D54-BED2-4167-A5E8-E0206520218A}' },
    pattern: [ '*7XXXXXXX' ],
    description: [ 'DNR_21' ],
    routePartitionName: [ 'DNR_21-PT' ] } ]

I have tried to use NPM modules array-query and json-query and neither work. I believe I am either doing something wrong to query the data, or I have dirty data that needs to be cleaned up.

Can anyone verify:

  1. Is the data a JS array or dirty?
  2. A query that uses the logic. (If "routePartitionName" = "DHHS_62-PT" then return "pattern"?

I think I'm way off in the weeds on this, and can't seem to find my way back to the road. I appreciate any help or advice!

2 Answers

the provided javascript is valid. Here is the filter function that goes through the whole object and returns a set of found patterns for a given routeParameterName.

test data

const obj = [{
        '$': {
            uuid: '{461F892D-07D8-4FCA-AF86-D5A9CBB080B8}'
        },
        pattern: ['*689XXXX'],
        description: ['DHHS_62'],
        routePartitionName: ['DHHS_62-PT']
    },
    {
        '$': {
            uuid: '{84651C57-5E4C-4981-B859-38C8ECC56E3B}'
        },
        pattern: ['*745XXXX'],
        description: ['DHHS_43'],
        routePartitionName: ['DHHS_43-PT']
    },
    {
        '$': {
            uuid: '{BC3259C4-38DB-4AD6-8594-61B938E4542C}'
        },
        pattern: ['*753XXXX'],
        description: ['DEQ_50'],
        routePartitionName: ['DEQ_50-PT']
    },
    {
        '$': {
            uuid: '{4CF32D54-BED2-4167-A5E8-E0206520218A}'
        },
        pattern: ['*7XXXXXXX'],
        description: ['DNR_21'],
        routePartitionName: ['DNR_21-PT']
    },
    {
        '$': {
            uuid: '{4CF32D54-BED2-4167-A5E8-E0206520218A}'
        },
        pattern: ['*6XXXXXX'],
        description: ['DNR_21'],
        routePartitionName: ['DNR_21-PT']
    }
];

function

function filter(data, key) {
    var patterns = [];

    for (var i = 0; i < data.length; i++) {
        var routePartitionNames = data[i].routePartitionName;
        var currentPatterns = data[i].pattern;

        if (!Array.isArray(routePartitionNames) ||
            routePartitionNames.length === 0 ||
            routePartitionNames.indexOf(key) < 0
        ) {
            continue;
        }

        // key found in current routePartitionName
        for (var j = 0; j < currentPatterns.length; j++) {
            var currentPattern = currentPatterns[j];
            if (patterns.indexOf(currentPattern) < 0) {
                patterns.push(currentPattern);
            }
        }
    }
    return patterns;
}

usage: filter(data, 'DNR_21-PT') returns: ["*7XXXXXXX", "*6XXXXXX"]

You can use filter to filter the object based on routePartitionName and then pass that to a map to get the desired pattern. There is probably a better way this is just quick and dirty.

const obj = [ { '$': { uuid: '{461F892D-07D8-4FCA-AF86-D5A9CBB080B8}' },
    pattern: [ '*689XXXX' ],
    description: [ 'DHHS_62' ],
    routePartitionName: [ 'DHHS_62-PT' ] },
  { '$': { uuid: '{84651C57-5E4C-4981-B859-38C8ECC56E3B}' },
    pattern: [ '*745XXXX' ],
    description: [ 'DHHS_43' ],
    routePartitionName: [ 'DHHS_43-PT' ] },
  { '$': { uuid: '{BC3259C4-38DB-4AD6-8594-61B938E4542C}' },
    pattern: [ '*753XXXX' ],
    description: [ 'DEQ_50' ],
    routePartitionName: [ 'DEQ_50-PT' ] },
  { '$': { uuid: '{4CF32D54-BED2-4167-A5E8-E0206520218A}' },
    pattern: [ '*7XXXXXXX' ],
    description: [ 'DNR_21' ],
    routePartitionName: [ 'DNR_21-PT' ] } ]

let obj2 = obj.filter(item => item.routePartitionName == 'DHHS_62-PT').map(id => id.pattern);

console.log(obj2);

Related