Hello I need to check 3 - dimensional json array, now I check it like this
if(events[dd][mm][rr] !== undefined){}
but if keys doesn't exists it throw's me an error. TypeError: events[dd] is undefined
I need some JS function to check if this condition is exists and throught error of TypeError. Thank you.
You'll have to check for each nested namespace. You could also write a recursive function if needed, if you have to check deeper into the map later on.
if (events[dd] && events[dd][mm] && events[dd][mm][rr] !== undefined) {}
Maybe use something like this:
function mdArrayExists(arr, var_args) {
for (var i=1, k=arguments.length; i<k; ++i) {
if (!arr || !arr.hasOwnProperty(arguments[i])
return false;
arr = arr[arguments[i]];
}
return true;
}
Usage:
if (mdArrayExists(events, dd, mm, rr) ...
我与这家公司没有任何隶属关系,但是每当我需要进行数组搜索时,我都会使用underscore.js库来执行此操作,因为该库具有许多用于快速查找数据的功能,并且性能明智的做法是节省开销如果我必须自己遍历数组。
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.