简体   繁体   中英

How do I dynamically check an object for a subobject/property in javascript/jquery

I am using the following code to build object for posting via ajax:

eval('var prop = { ' + input.attr('name') + ': inputVal };');
paramsObj = $.extend(paramsObj, prop);

Unfortunately I am having a little trouble when parsing my form when it comes to radios/checkboxes as I need to concatinate the values into a single param.

How can I check if paramsObj has an object called input.attr('name'), so I can change it rather than setting it

Thanks

var propName = input.attr('name');
if (paramsObj.hasOwnProperty(propName)) {
    console.log('prop found');
} else {
    console.log('prop not found');
}

PS. Don't use eval. Ever.

You can get the same effect you have in eval with following code and it's much safer:

var prop = {};
prop[input.attr('name')] = inputVal;

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