Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read JS object without case sensitivity

Tags:

javascript

Lets say we have a Product object with Product.Name, Product.Desc, and Product.Price

but for reasons beyond our control we might recieve a product object with lowercase variables (Product.name, Product.desc, Product.price)

is there a way to interpret variables that are not case sensitive? Or do I have to do some regex .toLowerCase() magic?

Thoughts?

like image 733
stackoverfloweth Avatar asked Nov 27 '25 01:11

stackoverfloweth


1 Answers

I like Alex Filatov's solution (+1). Yet, sometimes the name variations are known in advance and/or you may want to accept only certain variations. In this case I've found it easier to do this:

 Product.Name = Product.Name || Product.name || default.Name;

Just OR the acceptable name variations and optionally add a default value.

like image 145
Roberto Avatar answered Nov 29 '25 15:11

Roberto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!