I want use dynamic {i} in loop for access obj array after parsing like modele_{i}.view_[i] for access values please help me to solve my problem
how can replace this line
var Ob = obj.permissions[i-1].module_1.view_1;
With
var Ob = obj.permissions[i-1].module_{i}.view_{i};
const data = '{"permissions": [{"module_1": {"view_1":"false","edit_1":"false","delete_1":"false","add_1":"false","search_1":"false"}},{"module_2": {"view_2":"false","edit_2":"false","delete_2":"false","add_2":"false","search_2":"false"}},{"module_3": {"view_3":"false","edit_3":"false","delete_3":"false","add_3":"false","search_3":"false"}},{"module_4": {"view_4":"false","edit_4":"false","delete_4":"false","add_4":"false","search_4":"false"}},{"module_5": {"view_5":"false","edit_5":"false","delete_5":"false","add_5":"false","search_5":"false"}},{"module_6": {"view_6":"false","edit_6":"false","delete_6":"false","add_6":"false","search_6":"false"}},{"module_7": {"view_7":"false","edit_7":"false","delete_7":"false","add_7":"false","search_7":"false"}},{"module_8": {"view_8":"false","edit_8":"false","delete_8":"false","add_8":"false","search_8":"false"}},{"module_9": {"view_9":"false","edit_9":"false","delete_9":"false","add_9":"false","search_9":"false"}},{"module_10": {"view_10":"false","edit_10":"false","delete_10":"false","add_10":"false","search_10":"false"}},{"module_11": {"view_11":"false","edit_11":"false","delete_11":"false","add_11":"false","search_11":"false"}},{"module_12": {"view_12":"false","edit_12":"false","delete_12":"false","add_12":"false","search_12":"false"}},{"module_13": {"view_13":"false","edit_13":"false","delete_13":"false","add_13":"false","search_13":"false"}},{"module_14": {"view_14":"false","edit_14":"false","delete_14":"false","add_14":"false","search_14":"false"}},{"module_15": {"view_15":"false","edit_15":"false","delete_15":"false","add_15":"false","search_15":"false"}},{"module_16": {"view_16":"false","edit_16":"false","delete_16":"false","add_16":"false","search_16":"false"}},{"module_17": {"view_17":"false","edit_17":"false","delete_17":"false","add_17":"false","search_17":"false"}},{"module_18": {"view_18":"false","edit_18":"false","delete_18":"false","add_18":"false","search_18":"false"}},{"module_19": {"view_19":"false","edit_19":"false","delete_19":"false","add_19":"false","search_19":"false"}},{"module_20": {"view_20":"false","edit_20":"false","delete_20":"false","add_20":"false","search_20":"false"}},{"module_21": {"view_21":"false","edit_21":"false","delete_21":"false","add_21":"false","search_21":"false"}},{"module_22": {"view_22":"false","edit_22":"false","delete_22":"false","add_22":"false","search_22":"false"}},{"module_23": {"view_23":"false","edit_23":"false","delete_23":"false","add_23":"false","search_23":"false"}},{"module_24": {"view_24":"false","edit_24":"false","delete_24":"false","add_24":"false","search_24":"false"}},{"module_25": {"view_25":"false","edit_25":"false","delete_25":"false","add_25":"false","search_25":"false"}},{"module_26": {"view_26":"false","edit_26":"false","delete_26":"false","add_26":"false","search_26":"false"}},{"module_27": {"view_27":"false","edit_27":"false","delete_27":"false","add_27":"false","search_27":"false"}},{"module_28": {"view_28":"false","edit_28":"false","delete_28":"false","add_28":"false","search_28":"false"}},{"module_29": {"view_29":"false","edit_29":"false","delete_29":"false","add_29":"false","search_29":"false"}},{"module_30": {"view_30":"false","edit_30":"false","delete_30":"false","add_30":"false","search_30":"false"}},{"module_31": {"view_31":"false","edit_31":"false","delete_31":"false","add_31":"false","search_31":"false"}},{"module_32": {"view_32":"false","edit_32":"false","delete_32":"false","add_32":"false","search_32":"false"}},{"module_33": {"view_33":"false","edit_33":"false","delete_33":"false","add_33":"false","search_33":"false"}},{"module_34": {"view_34":"false","edit_34":"false","delete_34":"false","add_34":"false","search_34":"false"}},{"module_35": {"view_35":"false","edit_35":"false","delete_35":"false","add_35":"false","search_35":"false"}},{"module_36": {"view_36":"false","edit_36":"false","delete_36":"false","add_36":"false","search_36":"false"}},{"module_37": {"view_37":"false","edit_37":"false","delete_37":"false","add_37":"false","search_37":"false"}},{"module_38": {"view_38":"false","edit_38":"false","delete_38":"false","add_38":"false","search_38":"false"}},{"module_39": {"view_39":"false","edit_39":"false","delete_39":"false","add_39":"false","search_39":"false"}},{"module_40": {"view_40":"false","edit_40":"false","delete_40":"false","add_40":"false","search_40":"false"}},{"module_41": {"view_41":"false","edit_41":"false","delete_41":"false","add_41":"false","search_41":"false"}},{"module_42": {"view_42":"false","edit_42":"false","delete_42":"false","add_42":"false","search_42":"false"}},{"module_43": {"view_43":"false","edit_43":"false","delete_43":"false","add_43":"false","search_43":"false"}},{"module_44": {"view_44":"false","edit_44":"false","delete_44":"false","add_44":"false","search_44":"false"}},{"module_45": {"view_45":"false","edit_45":"false","delete_45":"false","add_45":"false","search_45":"false"}},{"module_46": {"view_46":"false","edit_46":"false","delete_46":"false","add_46":"false","search_46":"false"}},{"module_47": {"view_47":"false","edit_47":"false","delete_47":"false","add_47":"false","search_47":"false"}},{"module_48": {"view_48":"false","edit_48":"false","delete_48":"false","add_48":"false","search_48":"false"}},{"module_49": {"view_49":"false","edit_49":"false","delete_49":"false","add_49":"false","search_49":"false"}},{"module_50": {"view_50":"false","edit_50":"false","delete_50":"false","add_50":"false","search_50":"false"}},{"module_51": {"view_51":"false","edit_51":"false","delete_51":"false","add_51":"false","search_51":"false"}},{"module_52": {"view_52":"false","edit_52":"false","delete_52":"false","add_52":"false","search_52":"false"}},{"module_53": {"view_53":"false","edit_53":"false","delete_53":"false","add_53":"false","search_53":"false"}},{"module_54": {"view_54":"false","edit_54":"false","delete_54":"false","add_54":"false","search_54":"false"}}],"count":"54"}';
const obj = JSON.parse(data);
let Size = obj.count;
for (let i=1; i <= Size ; i ) {
var Ob = obj.permissions[i-1].module_1.view_1;
console.log(Ob);
}
CodePudding user response:
Since you only have one object that matches you don't need a loop. Just find
the first object that matches.
const data = '{"permissions": [{"module_1": {"view_1":"false","edit_1":"false","delete_1":"false","add_1":"false","search_1":"false"}},{"module_2": {"view_2":"false","edit_2":"false","delete_2":"false","add_2":"false","search_2":"false"}},{"module_3": {"view_3":"false","edit_3":"false","delete_3":"false","add_3":"false","search_3":"false"}},{"module_4": {"view_4":"false","edit_4":"false","delete_4":"false","add_4":"false","search_4":"false"}},{"module_5": {"view_5":"false","edit_5":"false","delete_5":"false","add_5":"false","search_5":"false"}},{"module_6": {"view_6":"false","edit_6":"false","delete_6":"false","add_6":"false","search_6":"false"}},{"module_7": {"view_7":"false","edit_7":"false","delete_7":"false","add_7":"false","search_7":"false"}},{"module_8": {"view_8":"false","edit_8":"false","delete_8":"false","add_8":"false","search_8":"false"}},{"module_9": {"view_9":"false","edit_9":"false","delete_9":"false","add_9":"false","search_9":"false"}},{"module_10": {"view_10":"false","edit_10":"false","delete_10":"false","add_10":"false","search_10":"false"}},{"module_11": {"view_11":"false","edit_11":"false","delete_11":"false","add_11":"false","search_11":"false"}},{"module_12": {"view_12":"false","edit_12":"false","delete_12":"false","add_12":"false","search_12":"false"}},{"module_13": {"view_13":"false","edit_13":"false","delete_13":"false","add_13":"false","search_13":"false"}},{"module_14": {"view_14":"false","edit_14":"false","delete_14":"false","add_14":"false","search_14":"false"}},{"module_15": {"view_15":"false","edit_15":"false","delete_15":"false","add_15":"false","search_15":"false"}},{"module_16": {"view_16":"false","edit_16":"false","delete_16":"false","add_16":"false","search_16":"false"}},{"module_17": {"view_17":"false","edit_17":"false","delete_17":"false","add_17":"false","search_17":"false"}},{"module_18": {"view_18":"false","edit_18":"false","delete_18":"false","add_18":"false","search_18":"false"}},{"module_19": {"view_19":"false","edit_19":"false","delete_19":"false","add_19":"false","search_19":"false"}},{"module_20": {"view_20":"false","edit_20":"false","delete_20":"false","add_20":"false","search_20":"false"}},{"module_21": {"view_21":"false","edit_21":"false","delete_21":"false","add_21":"false","search_21":"false"}},{"module_22": {"view_22":"false","edit_22":"false","delete_22":"false","add_22":"false","search_22":"false"}},{"module_23": {"view_23":"false","edit_23":"false","delete_23":"false","add_23":"false","search_23":"false"}},{"module_24": {"view_24":"false","edit_24":"false","delete_24":"false","add_24":"false","search_24":"false"}},{"module_25": {"view_25":"false","edit_25":"false","delete_25":"false","add_25":"false","search_25":"false"}},{"module_26": {"view_26":"false","edit_26":"false","delete_26":"false","add_26":"false","search_26":"false"}},{"module_27": {"view_27":"false","edit_27":"false","delete_27":"false","add_27":"false","search_27":"false"}},{"module_28": {"view_28":"false","edit_28":"false","delete_28":"false","add_28":"false","search_28":"false"}},{"module_29": {"view_29":"false","edit_29":"false","delete_29":"false","add_29":"false","search_29":"false"}},{"module_30": {"view_30":"false","edit_30":"false","delete_30":"false","add_30":"false","search_30":"false"}},{"module_31": {"view_31":"false","edit_31":"false","delete_31":"false","add_31":"false","search_31":"false"}},{"module_32": {"view_32":"false","edit_32":"false","delete_32":"false","add_32":"false","search_32":"false"}},{"module_33": {"view_33":"false","edit_33":"false","delete_33":"false","add_33":"false","search_33":"false"}},{"module_34": {"view_34":"false","edit_34":"false","delete_34":"false","add_34":"false","search_34":"false"}},{"module_35": {"view_35":"false","edit_35":"false","delete_35":"false","add_35":"false","search_35":"false"}},{"module_36": {"view_36":"false","edit_36":"false","delete_36":"false","add_36":"false","search_36":"false"}},{"module_37": {"view_37":"false","edit_37":"false","delete_37":"false","add_37":"false","search_37":"false"}},{"module_38": {"view_38":"false","edit_38":"false","delete_38":"false","add_38":"false","search_38":"false"}},{"module_39": {"view_39":"false","edit_39":"false","delete_39":"false","add_39":"false","search_39":"false"}},{"module_40": {"view_40":"false","edit_40":"false","delete_40":"false","add_40":"false","search_40":"false"}},{"module_41": {"view_41":"false","edit_41":"false","delete_41":"false","add_41":"false","search_41":"false"}},{"module_42": {"view_42":"false","edit_42":"false","delete_42":"false","add_42":"false","search_42":"false"}},{"module_43": {"view_43":"false","edit_43":"false","delete_43":"false","add_43":"false","search_43":"false"}},{"module_44": {"view_44":"false","edit_44":"false","delete_44":"false","add_44":"false","search_44":"false"}},{"module_45": {"view_45":"false","edit_45":"false","delete_45":"false","add_45":"false","search_45":"false"}},{"module_46": {"view_46":"false","edit_46":"false","delete_46":"false","add_46":"false","search_46":"false"}},{"module_47": {"view_47":"false","edit_47":"false","delete_47":"false","add_47":"false","search_47":"false"}},{"module_48": {"view_48":"false","edit_48":"false","delete_48":"false","add_48":"false","search_48":"false"}},{"module_49": {"view_49":"false","edit_49":"false","delete_49":"false","add_49":"false","search_49":"false"}},{"module_50": {"view_50":"false","edit_50":"false","delete_50":"false","add_50":"false","search_50":"false"}},{"module_51": {"view_51":"false","edit_51":"false","delete_51":"false","add_51":"false","search_51":"false"}},{"module_52": {"view_52":"false","edit_52":"false","delete_52":"false","add_52":"false","search_52":"false"}},{"module_53": {"view_53":"false","edit_53":"false","delete_53":"false","add_53":"false","search_53":"false"}},{"module_54": {"view_54":"false","edit_54":"false","delete_54":"false","add_54":"false","search_54":"false"}}],"count":"54"}';
const obj = JSON.parse(data);
const number = 1;
const ob = obj.permissions.find(obj => {
return obj[`module_${number}`][`view_${number}`];
});
console.log(ob);