How to get key of a json which resides within a array based on value, say if the value as ValueB it 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 anyone please tell me some solution for this