javascript - Get value of an array based on priority -
i have object array based on priority need form object array. below code tried
var data_res = [{ "action_type": "create", "execution_type": "n_execution" }, { "action_type": "create", "execution_type": "r_pre_execution" } ]; var arr = ["r_pre_execution", "s_pre_execution", "n_execution"]; (var = 0; < data_res.length; i++) { (var temp in arr) { if (data_res[i].rule_execution_type.indexof(arr[temp])>-1) { console.log("data") console.log(json.stringify(data_res[temp])) } }
the output i'm getting is
data undefined data {"action_type":"create","execution_type":"n_execution"}
i want data based on array arr
. first r_pre_execution
data must formed s_pre_execution
, n_pre_execution
, don't know i'm going wrong.
it looks want sort data_res
according order given arr
.
var data_res = [{ "action_type": "create", "execution_type": "n_execution" }, { "action_type": "create", "execution_type": "r_pre_execution" }] var arr = ["r_pre_execution", "s_pre_execution", "n_execution"]; data_res.sort(function(a, b) { var akey = arr.indexof(a.execution_type); var bkey = arr.indexof(b.execution_type); return akey - bkey; }); document.write("<pre>" + json.stringify(data_res,0,3));
Comments
Post a Comment