javascript 如何找出我推入数组的对象的索引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16139752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find out the index number of an object that I pushed into an array?
提问by jfriend00
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?
如何找出刚刚推入 gridData 对象的新数据的索引号?
回答by jfriend00
First off, I'll assume the gridData
is an array, not an object as you've shown in your sample code because an object doesn't have a .push()
method, but an array does.
首先,我假设gridData
是一个数组,而不是您在示例代码中显示的对象,因为对象没有.push()
方法,但数组有。
Use .length - 1
as the index to the last item you pushed onto the array or save the returned value from .push()
which is the new length of the array. This will be the index of the element that you just pushed onto the array and will be valid until you modify the array before that index (adding or removing items before that index).
使用.length - 1
作为索引到最后一个项目,你推到阵列或保存返回的值从.push()
哪个是数组的新长度。这将是您刚刚推入数组的元素的索引,并且在您修改该索引之前的数组(在该索引之前添加或删除项目)之前一直有效。
var testRowIndex = gridData.push(TestRow) - 1;
// then you can access that item like this
var item = gridData[testRowIndex];
Though, this doesn't make a whole lot of sense since you already have the data right in TestRow
. As usual, if you describe what problem you're really trying to solve, we can probably provide more useful answers.
不过,这并没有多大意义,因为您已经在TestRow
. 像往常一样,如果您描述了您真正想要解决的问题,我们可能会提供更有用的答案。
回答by PSL
When you push the item into an array you will get the current length of the array as return value. Use it to find the index.
当您将项目推入数组时,您将获得数组的当前长度作为返回值。使用它来查找索引。
var gridData = [];
var TestRow = {
"name": "xx",
"description": "xx",
"subjectId": 15
};
var length=gridData.push(TestRow);
alert(length-1);
Array.pushreturns The new length property of the object upon which the method was called.
Array.push返回调用该方法的对象的新长度属性。
回答by Shubh
First go, i'll say it's similar to find-indexof-element-in-jquery-array
首先,我会说它类似于find-indexof-element-in-jquery-array
Anyhow, saw @jfriend00
and @PSCoder
answering it brilliantly, I wanted to convey some alternative's to Find Index,
无论如何,看到@jfriend00
并@PSCoder
出色地回答了它,我想传达一些替代查找索引的方法,
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
我有两个或更多的数据 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
现在,我推送这两个数据,就像您所做的那样。要找到被推送元素的索引,我们可以使用,.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);
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>