Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我怎样才能找到我放入数组的对象的索引号?_Javascript - Fatal编程技术网

Javascript 我怎样才能找到我放入数组的对象的索引号?

Javascript 我怎样才能找到我放入数组的对象的索引号?,javascript,Javascript,我有以下资料: var gridData = {}; var TestRow = { "name": "xx", "description": "xx", "subjectId": 15 }; gridData.push(TestRow) 我怎样才能找到新数据的索引号 我刚刚插入了gridData对象?首先,我假设gridData是一个数组,而不是像示例代码中所示的对象,因为对象没有.push()方法,但数组有 使用.length-1作为最后一

我有以下资料:

var gridData = {};
var TestRow = {
   "name": "xx",
   "description": "xx",
   "subjectId": 15
                };
gridData.push(TestRow)
我怎样才能找到新数据的索引号
我刚刚插入了gridData对象?

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

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


但是,这没有多大意义,因为您已经在
TestRow
中拥有了正确的数据。通常,如果您描述了您真正想要解决的问题,我们可能会提供更有用的答案。

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

var gridData = [];
var TestRow = {
   "name": "xx",
   "description": "xx",
   "subjectId": 15
                };
var length=gridData.push(TestRow);
alert(length-1);
返回调用该方法的对象的新长度属性


首先,我会说它类似于

总之,看到
@jfriend00
@PSCoder
出色地回答了这个问题,我想传达一些备选方案来查找索引

假设您的数组为:-

var gridData = [];//{} Curly braces will define it as object 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
    };
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);
现在,我推送这两个数据,就像你所做的那样。要查找被推送元素的索引,我们可以使用,
.indexOf
.inArray

var TestRow = {
        "name": "xx",
        "description": "xx",
        "subjectId": 15
    };
    var TestRow1 = {
        "name": "xx1",
        "description": "xx1",
        "subjectId": 151
    };
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>

$(文档).ready(函数(){
var gridData=[];//{}大括号将其定义为Boject类型,可以对数组的
var TestRow={
“名称”:“xx”,
“说明”:“xx”,
“主观性”:15
};
var TestRow1={
“名称”:“xx1”,
“说明”:“xx1”,
“主观性”:151
};
push(TestRow);
gridData.push(TestRow1);
console.log(gridData);
var indexofestrow0=gridData.indexOf(TestRow);//如果元素存在,则返回该元素的索引;如果元素不存在,则返回-1。
var indexofestrow1=gridData.indexOf(TestRow1);//如果元素存在,则返回该元素的索引;如果元素不存在,则返回-1。
//在数组中搜索指定的值并返回其索引(如果未找到,则返回-1)。
var indx1=jQuery.inArray(TestRow,gridData);
var indx2=jQuery.inArray(TestRow1,gridData);
console.log(indexofestrow0);
console.log(indexofestrow1);
控制台日志(indx1);
控制台日志(indx2);
});

的可能重复您不想先找出该对象上不存在
push()
方法的原因吗?我不想重复这个答案。但是当我看到jfriend00通过计算array.length-1来回答这个问题时,我想到了使用返回值本身来回答这个快捷方式。我认为这不是一个很好的答案,因为长度不一定是索引——在其他东西也在操纵数组的情况下。在我的例子中,有一个从数组中删除项目的超时,因此长度与问题所问的索引不同。@PaulHaggo-如果将值推送到数组中,则
length-1
始终是您刚才推送到数组中的项目的索引。如果您有其他代码操纵数组,则所有赌注都将被取消-上一个项目的索引未知,并且不会被知道,因为它取决于其他代码对数组所做的操作。该问题要求提供“刚刚推送”的项目的索引。这就回答了这个问题。我知道你的意思,但是你得到的索引只有在索引之前没有从数组中删除任何项时才有效。我可以立即使用您的方法获取索引,但是如果我稍后尝试使用该值,它可能不会引用我认为它引用的元素。你的答案仍然是正确的,但我不认为它对那些不太了解数组的人有帮助。@PaulHaggo-是的。OP要求
“我刚刚推送到gridData对象中的新数据的索引号”
。请参见该语句中的“刚刚按下”
。在javascript中,无论在数组中的某个元素之前添加/删除了多少项,都无法获得始终指向该数组特定元素的动态索引。如果您想要类似的内容,那么您可能希望在对象中使用命名属性,而不是数组位置。@PaulHaggo-好的,有效点。我添加了一些更详细的内容,以明确只有在索引之前不插入/删除元素的情况下,此索引才是好的。这是此线程中唯一正确的答案。这应该是公认的答案。如果在当前接受的答案中数组已更改,则索引错误。主题是获取索引,而不是计数减去1。