javascript - get Json Key based on Json Value of an Object which resides in an Json Array -
how key of json resides within array based on value, if value valueb should return mainb
var app = angular.module('myapp', []); app.controller('arraycontroller', function ($scope) { $scope.mydatas = [ { "test1": "value1", "maina": "" }, { "test1": "value2", "maina": "valueb" }, { "test1": "", "maina": "valuec" } ]; $scope.getjsonkey = function(jsonarray, jsonvalue) { angular.foreach(jsonarray, function(value, index) { if (value.test1 === jsonvalue) { return "test1"; } if (value.maina === jsonvalue) { return "maina"; } }); }; console.log($scope.getjsonkey($scope.mydatas, "valueb")); });
can please tell me solution this
here small function should want, long values unique:
var arr=[ { "test1": "value1", "maina": "valuea" }, { "test2": "value2", "mainb": "valueb" }, { "test3": "value3", "mainc": "valuec" } ] function getkey(arr, val){ for(var i=0;i<arr.length;i++){ var item= arr[i]; for(var key in item){ if(val === item[key]) return key; } } return null; // not found } console.log(getkey(arr, 'valueb')) // mainb
Comments
Post a Comment