Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out the index number of an object that I pushed into an array?

Tags:

javascript

People also ask

How do you find the index number of an object in an array?

To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.

How do you find the index of an array element in C++?

Using std::find_if algorithm For instance, find the index of the first 2-digit number in the array. The recommended approach is to use the std::find_if algorithm, which accepts a predicate to handle such cases. That's all about finding the index of an element in an array in C++.

How do you add an object to an index of an array?

You want to explicitly add it at a particular place of the array. That place is called the index. Array indexes start from 0 , so if you want to add the item first, you'll use index 0 , in the second place the index is 1 , and so on. To perform this operation you will use the splice() method of an array.


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.

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).

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.


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.push returns The new length property of the object upon which the method was called.


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>