0

I have a project where I am working on conditional rules for a form. At this point I have two object where one are the conditions and the other are the filled in form fields which I need to check against the conditions.

I've got as far as making both of the objects, next logical step for me would be an if statement which checks whether or not the form object contains all of the condition object's values. (correct me if I am wrong)

I'll show both of my objects here:

let conditions = {
    carrier: "PostNL",
    country: "Deutschland"
}
let ticket_data = {
    carrier: "PostNL",
    client: "testdata",
    comment: "testdata",
    country: "Deutschland",
    postal_code: "testdata",
    service_level: "testdata",
}

As you can see, the object ticket_data contains all of the data the object conditions contains. In this case the if statement should return true. However, if one of the values wouldn't exist or wouldn't be equal to the conditions object it should return false.

What would be the best and easiest way to execute this?

1
  • 1
    Iterate the properties in your conditions object, check if that property exists inside your ticket, if not return false, else check if the value meets the criteria, if not return false, else continue loop. Commented Mar 1, 2019 at 16:55

4 Answers 4

2

You can take the keys and check every keys value.

let conditions = { carrier: "PostNL", country: "DeutschlandTEST" }
let ticket_data = {carrier: "PostNL",client: "testdata",comment: "testdata",country: "Deutschland",postal_code: "testdata",service_level: "testdata",}
  
let checkTrue = Object.keys(conditions).every( e => ticket_data[e] )
let matchValues = Object.keys(conditions).every( e =>conditions[e] === ticket_data[e] )


if(Object.keys(conditions).every( e => ticket_data[e] )){
 console.log(`Yeah it's working`) 
}

console.log(checkTrue, matchValues)

Sign up to request clarification or add additional context in comments.

4 Comments

This unfortunately always results in true. I edited the value Deutschland to DeutschlandTEST in one of the two objects and it still gave me true.
@mike you want to match values too ? conditions[e] === ticket_data[e] will be enough
Thanks, that's how I thought of it yes. Now, is it possible to make this into an if statement? So I have a block of code to execute when it returns false and when it returns true?
Could you edit your answer accordingly? Will be accepting it as the right answer,
2

Object keys/entries and every. Every loops over, check for a match. If match fails it stops looping.

let conditions = {
    carrier: "PostNL",
    country: "Deutschland"
}
let ticket_data = {
    carrier: "PostNL",
    client: "testdata",
    comment: "testdata",
    country: "Deutschland",
    postal_code: "testdata",
    service_level: "testdata",
}

const isMatch = Object.keys(conditions)
  .every(key => 
    conditions[key] === ticket_data[key])
    
console.log(isMatch)

const isMatch2 = Object.entries(conditions)
  .every(([key, value]) => 
    value === ticket_data[key])
    
console.log(isMatch2)

1 Comment

What happens if the given key doesn't exist in ticket_data ? Would that evaluate to false or throw an error?
1

Compare the Object.keys of both :

let conditions = {
    carrier: "PostNL",
    country: "Deutschland"
}

let ticket_data = {
    carrier: "PostNL",
    client: "testdata",
    comment: "testdata",
    country: "Deutschland",
    postal_code: "testdata",
    service_level: "testdata",
}

const ticket_data_keys = Object.keys(ticket_data);

const result = Object.keys(conditions).every(key => ticket_data_keys.includes(key));

console.log(result);

1 Comment

This unfortunately always results in true. I edited the value Deutschland to DeutschlandTEST in one of the two objects and it still gave me true
0

I'll do something like this:

let conditions = {
    carrier: "PostNL",
    country: "Deutschland"
}

let ticket_data = {
    carrier: "PostNL",
    client: "testdata",
    comment: "testdata",
    country: "Deutschland",
    postal_code: "testdata",
    service_level: "testdata",
}

const requiredProps = [
    'carrier', 'client', 'comment', 'country', 'postal_code', 'service_level'
];

const checkIfIsValid = (ticketData) => {
    const keys = Object.keys(ticketData);

    // Check for equality
    const areEquals = (keys.length == requiredProps.length) && keys.every(function(element, index) {
        return element === requiredProps[index]; 
    });

    if (!areEquals) {
        return false;
    }

    if (ticketData.carrier !== conditions.carrier) {
        return false;
    }

    if (ticketData.country !== conditions.country) {
        return false;
    }

    return true;
}

if (checkIfIsValid(ticket_data)) {
    console.log('OK');
} else {
    console.log('ERROR');
}

2 Comments

So far (quick testing) this answer is the only one that works as expected. But I would prefer it to be dynamic in stead of making a separate if statement for every comparison. Do you see an option to make this possible?
Yes @mike sure, you can do a foreach and compare those values with the one in ticketdata

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.