Home > Software design >  react check array values based on specific keys returning true/false if values found
react check array values based on specific keys returning true/false if values found

Time:10-14

I am trying to figure out how to do this but can't seem to wrap my head around it..

I have an address object

 const obj = {
   "address_type":"Home",
   "country":"US",
   "addressLine1":"123  Any Street",
   "addressLine2":"",
   "city":"Any Town",
   "state":"Indiana",
   "state_code":"IN",
   "zip":"46220-4466",
   "phone":"6715551313",
   "mobile_number":"",
   "extn":"",
   "fax":"",
   "county_name":"MyCounty"
}

I want to check for any key that has a value but only specific keys

const objProps = ["addressLine1","addressLine2","city","state_code","zip","county_name"];

I want to check all keys in objProps against my address object and if any one of them contains a value return true (doesn't matter if its 1 or all 6).. If all keys don't contain a value then return false (Sometimes I will get an address object that has all null values)

I've tried various ways to accomplish this but have failed in each one.

The variation I am working on now is using reduce. While it doesn't meet my needs I thought I could check the resulting array and if length was greater than 0 than I have my answer..

Work-in-progress:

function hasAddressData(obj: any) {
        const objProps = ["addressLine1","addressLine2","city","state_code","zip","county_name"];
        const keysWithData = objProps.reduce((accumulator, key) => {
            const propExistsOnObj = obj.hasOwnProperty(key);
            let keyHasData = [];
            if (obj[key].length > 0 ) {
                keyHasData = obj[key]
            }

            if (!propExistsOnObj) {
                accumulator.push(key);
            } else if (keyHasData) {
                const equalValueKeyIndex = accumulator.indexOf(key);
                accumulator.splice(equalValueKeyIndex, 1);
            }
            return accumulator;
        });

        return keysWithData;
    }

The above is messed up I know and doesn't work.. Just learning this stuff.. anyone have a suggestion or comment?

CodePudding user response:

Check that .some of the objProps, when looked up on the obj, contain a value. (Either with Boolean or by comparing against '')

const obj = {
   "address_type":"Home",
   "country":"US",
   "addressLine1":"123  Any Street",
   "addressLine2":"",
   "city":"Any Town",
   "state":"Indiana",
   "state_code":"IN",
   "zip":"46220-4466",
   "phone":"6715551313",
   "mobile_number":"",
   "extn":"",
   "fax":"",
   "county_name":"MyCounty"
}
const objProps = ["addressLine1","addressLine2","city","state_code","zip","county_name"];

const somePopulated = objProps.some(prop => obj[prop]);
// or prop => obj[prop] !== ''
console.log(somePopulated);

const obj = {
   "address_type":"Home",
   "country":"US",
   "addressLine1":"",
   "addressLine2":"",
   "city":"",
   "state":"Indiana",
   "state_code":"",
   "zip":"",
   "phone":"6715551313",
   "mobile_number":"",
   "extn":"",
   "fax":"",
   "county_name":""
}
const objProps = ["addressLine1","addressLine2","city","state_code","zip","county_name"];

const somePopulated = objProps.some(prop => obj[prop]);
// or prop => obj[prop] !== ''
console.log(somePopulated);

CodePudding user response:

function checkKeys(target, props) {
  return props.some((prop) => {
    return target.hasOwnProperty(prop) && target[prop];
  });
}

Explanation: some iterates through the props you want to check, returning true immediately when one is found (i.e. the callback returns true). If no props are found (i.e. no callback returns true), some returns false.

hasOwnProperty ensures that you are only checking properties on target, and not looking up the prototype chain. target[prop] checks for a truthy value. You may need to modify this last check if you're going to be handling values other than strings.

  • Related