35

I have the following:

var gridData = {};
var TestRow = {
   "name": "xx",
   "description": "xx",
   "subjectId": 15
                };
gridData.push(TestRow)

How can I find out the index number of the new data that I just pushed into the gridData object?

4

3 回答 3

72

首先,我假设它gridData是一个数组,而不是您在示例代码中显示的对象,因为对象没有.push()方法,但数组有。

用作.length - 1您推入数组的最后一项的索引,或保存返回的值,.push()该值是数组的新长度。这将是您刚刚推入数组的元素的索引,并且在您修改该索引之前的数组(在该索引之前添加或删除项目)之前一直有效。

var testRowIndex = gridData.push(TestRow) - 1;
// then you can access that item like this 
var item = gridData[testRowIndex];

但是,这并没有多大意义,因为您已经在TestRow. 像往常一样,如果您描述您真正想要解决的问题,我们可能会提供更有用的答案。

于 2013-04-22T05:07:53.720 回答
8

当您将项目推入数组时,您将获得数组的当前长度作为返回值。使用它来查找索引。

var gridData = [];
var TestRow = {
   "name": "xx",
   "description": "xx",
   "subjectId": 15
                };
var length=gridData.push(TestRow);
alert(length-1);

Array.push 返回调用该方法的对象的新长度属性。

于 2013-04-22T05:08:53.420 回答
3

首先,我会说它类似于find-indexof-element-in-jquery-array

无论如何,看到@jfriend00@PSCoder出色地回答了它,我想向 Find Index 传达一些替代方案,

假设,您的数组为:-

var gridData = [];//{} Curly braces will define it as object type, push operations can take place with respect to Array's

我有两个或更多的数据Array

var TestRow = {
        "name": "xx",
        "description": "xx",
        "subjectId": 15
    };
    var TestRow1 = {
        "name": "xx1",
        "description": "xx1",
        "subjectId": 151
    };

现在,我推送这两个数据,就像你所做的那样。要找到被推送元素的索引,我们可以使用,.indexOf.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);

考虑测试这些东西,所以我尝试了一些非常简单的方法,如下所示: -

<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>
于 2013-04-22T05:41:55.463 回答