1

Let's there is an array & object.

getSelectedArrValue = ['firstName', 'lastName'];
getSelectedObjValue = {firstName: 'John', lastName: null, birthYear: 1980 };

getSelectedArrValue & getSelectedObjValue is generated by some another events and dynamically changed based on the event. Now, I want if the values of getSelectedArrValue exist in the object, getSelectedObjValue and any of them is null in the object, it'll show in console which one is null; something like this:

console.log('OOPS! ' + ____ + ' is null.');

So, the output for those selected data:

OOPS! lastName is null.

As, the values of the variable are generated continuously & dynamically based on some changed state(event), these can be happened:

getSelectedArrValue = ['firstName', 'lastName'];
getSelectedObjValue = {firstName: 'null', lastName: null, birthYear: 1985 };

Output:

OOPS! firstName is null.
OOPS! lastName is null.

Some more example:

getSelectedArrValue = ['firstName', 'lastName'];
getSelectedObjValue = {firstName: 'Mike', lastName: 'Hussy', birthYear: 1990 };

Output:

Everything is fine!

Another example:

getSelectedArrValue = ['phoneNumber', 'address'];
getSelectedObjValue = {firstName: 'Mike', lastName: 'Hussy', birthYear: 1990 };

Output:

Everything is fine! 

Please, help me to write the jQuery code to achieve those functionality. I guess, the js will be something like this:

for (var i = 0; i < getSelectedArrValue.length; i++) {
 if (getSelectedArrValue[i] in getSelectedObjValue.i) {
 // I am not sure that 'i' in object (getSelectedObjValue.i) works or not at here. 
 // Is there any other method to access value/key of object?
 if (getSelectedObjValue.i === null) {
 console.log('OOPS! ' + getSelectedArrValue[i] + ' is null.');
 } else {
 console.log('Everything is fine!');
 }
 }
}

But, I can't write in correct way as I am not good enough in jQuery. That's why I ask for help. Thanks in advance!

asked May 25, 2017 at 17:48

1 Answer 1

1

You can use the hasOwnProperty check on your object, for each array value, like so:

for (var i = 0; i < getSelectedArrValue.length; i++) {
 if (!getSelectedObjValue.hasOwnProperty(getSelectedArrValue[i]) || getSelectedObjValue[getSelectedArrValue[i]] === null) {
 console.log('OOPS! ' + getSelectedArrValue[i] + ' is null.');
 } else {
 console.log('Everything is fine!');
 }
}

EDIT: If you want the code to output everything is fine in case the object does NOT have a property in the array, you need to modify your if condition as such:

getSelectedObjValue.hasOwnProperty(getSelectedArrValue[i]) && getSelectedObjValue[getSelectedArrValue[i]] === null

answered May 25, 2017 at 17:51
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.