Idź, powiem, że to podobne do find-indexof-element-in-jquery-array
Zresztą widział @jfriend00
i @PSCoder
odpowiadając go znakomicie, chciałem przekazać niektóre alternatywa do znalezienia Index,
Zakładając, masz tablica jak: -
var gridData = [];//{} Curly braces will define it as object type, push operations can take place with respect to Array's
i posiada dwie lub więcej danych w tym Array
var TestRow = {
"name": "xx",
"description": "xx",
"subjectId": 15
};
var TestRow1 = {
"name": "xx1",
"description": "xx1",
"subjectId": 151
};
Teraz, popycham te dwie dane, tak jak to zrobiliście. Aby znaleźć indeks popychane elementu, możemy użyć, .indexOf
i .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);
Myśl testowania rzeczy, więc próbowałem coś bardzo prostego, jak poniżej: -
<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>
możliwy duplikat [JavaScript znajdź indeks tablicy z wartością] (http://stackoverflow.com/questions/7346827/javascript-find-array-index-with-value) –
Czy nie chcesz dowiedzieć się, dlaczego metoda 'push()' doesn ' t istnieć na tym obiekcie w pierwszej kolejności? – alex