First go, i'll say it's similar to find-indexof-element-in-jquery-array
Anyhow, saw @jfriend00
and @PSCoder
answering it brilliantly, I wanted to convey some alternative's to Find Index,
Assuming, you have your array as :-
var gridData = [];//{} Curly braces will define it as object type, push operations can take place with respect to Array's
and I have two or more data in that Array
var TestRow = {
"name": "xx",
"description": "xx",
"subjectId": 15
};
var TestRow1 = {
"name": "xx1",
"description": "xx1",
"subjectId": 151
};
Now, I push these two data, like the way you have done. To find the Index of the pushed element, we can use, .indexOf
and .inArray
var indexOfTestRow0 = gridData.indexOf(TestRow);// it returns the index of the element if it exists, and -1 if it doesn't.
var indexOfTestRow1 = gridData.indexOf(TestRow1);// it returns the index of the element if it exists, and -1 if it doesn't.
//Search for a specified value within an array and return its index (or -1 if not found).
var indx1 = jQuery.inArray(TestRow, gridData);
var indx2 = jQuery.inArray(TestRow1, gridData);
Thought of testing the stuff, so i tried something very simple like below:-
<head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<script>
$(document).ready(function () {
var gridData = [];//{} Curly braces will define it as Boject type, push operations can take place with respect to Array's
var TestRow = {
"name": "xx",
"description": "xx",
"subjectId": 15
};
var TestRow1 = {
"name": "xx1",
"description": "xx1",
"subjectId": 151
};
gridData.push(TestRow);
gridData.push(TestRow1);
console.log(gridData);
var indexOfTestRow0 = gridData.indexOf(TestRow);// it returns the index of the element if it exists, and -1 if it doesn't.
var indexOfTestRow1 = gridData.indexOf(TestRow1);// it returns the index of the element if it exists, and -1 if it doesn't.
//Search for a specified value within an array and return its index (or -1 if not found).
var indx1 = jQuery.inArray(TestRow, gridData);
var indx2 = jQuery.inArray(TestRow1, gridData);
console.log(indexOfTestRow0);
console.log(indexOfTestRow1);
console.log(indx1);
console.log(indx2);
});
</script>