JavaScript Get the index of an object by its property
Last Updated :
09 Jan, 2024
Given an object, the task is to get the object's index from the array of objects of the given property name and property value using JavaScript. we're going to discuss a few techniques.
Below are the following approaches:
This method creates a new array with the return value of calling a function for every array element. This method calls the provided function once for each element in an array, with maintaining the order.
Syntax:
array.map(function(currentValue, index, arr), thisValue)
Parameters:
- function(currentValue, index, arr): This parameter is required. It specifies a function to be run for each element in the array.
- currentValue: This parameter is required. It specifies the value of the current element.
- index: This parameter is optional. It specifies the array index of the current element.
- arr: This parameter is optional. It specifies the array object to which the current element belongs.
- thisValue: This parameter is optional. it specifies a value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed.
Example: This example uses the JavaScript Array map() Method to get the object's index with a given property.
JavaScript
let arrayObj = [{
prop_1: 'val',
prop_2: 'val_12',
prop_3: 'val_13'
}, {
prop_1: 'val',
prop_2: 'val_22',
prop_3: 'val_23'
}];
function GFG_Fun() {
let prop = 'prop_2';
let val = 'val_22';
console.log("Index of prop = "
+ prop + " val = " + val +
" is = " +
arrayObj.map(function (e) {
return e.prop_2;
}).indexOf(val));
}
GFG_Fun();
OutputIndex of prop = prop_2 val = val_22 is = 1
Using for loop we can iterate over the array of objects and check the given value of prop matches or not.
Example 1: This example searches for the attribute name and its value in the array and if it gets it, It returns the index of an object otherwise returns -1.
JavaScript
let arrayObj = [{
prop_1: 'val',
prop_2: 'val_12',
prop_3: 'val_13'
}, {
prop_1: 'val',
prop_2: 'val_22',
prop_3: 'val_23'
}];
function fun_2(array, attr, value) {
for (let i = 0; i < array.length; i += 1) {
if (array[i][attr] === value) {
return i;
}
}
return -1;
}
function GFG_Fun() {
let prop = 'prop_2';
let val = 'val_22';
console.log("Index of prop = '" +
prop + "' val = '" + val + "' is = "
+ fun_2(arrayObj, prop, val));
}
GFG_Fun();
OutputIndex of prop = 'prop_2' val = 'val_22' is = 1
The Javascript Array.findIndex() method is used to return the first index of the element in a given array that satisfies the provided testing function (passed in by the user while calling). Otherwise, if no data is found then the value of -1 is returned.
Example: In this example, we have used findIndex() Method
JavaScript
let arrayObj = [{
prop_1: 'val',
prop_2: 'val_12',
prop_3: 'val_13'
}, {
prop_1: 'val',
prop_2: 'val_22',
prop_3: 'val_23'
}];
const index = arrayObj.findIndex(object => {
return object.prop_3 === 'val_23';
});
console.log(index);
The Javascript arr.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method.
Example: In this example we have used some() Method
JavaScript
let arrayObj = [{
prop_1: 'val',
prop_2: 'val_12',
prop_3: 'val_13'
}, {
prop_1: 'val',
prop_2: 'val_22',
prop_3: 'val_23'
}];
let index;
arrayObj.some((object, idx) => {
if (object.prop_2 === 'val_12') {
index = idx;
return true;
}
});
console.log(index);
Similar Reads
How to read properties of an Object in JavaScript ? Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 min read
How to get the size of a JavaScript object ? In this article, we will see the methods to find the size of a JavaScript object. These are the following ways to solve the problem: Table of Content Using Object.keys() methodUsing Object.objsize() methodUsing Object.entries() methodUsing Object.values() methodUsing Object.keys() methodWe can get t
2 min read
How to iterate over a JavaScript object ? Iteration involves looping through the object's properties one by one. Depending on the method used, you can access and manipulate different levels of properties efficiently. Here are several methods.There are many methods to iterate over an object which are discussed below: Table of ContentUsing fo
3 min read
How to Get all Property Values of a JavaScript Object without knowing the Keys? To get all property values from a JavaScript object without knowing the keys involves accessing the object's properties and extracting their values.Below are the approaches to get all property values of a JavaScript Object:Table of ContentUsing Object.values() MethodUsing Object.keys() methodApproac
2 min read
Find the Length of JavaScript object Finding the length of a JavaScript object refers to determining how many key-value pairs JavaScript object contains. This is often necessary when you need to know the size of the data structure for iterations, validations, or other operations involving object properties.1. Using the Object.keys() me
3 min read
JavaScript indexOf() method in an Object Array In JavaScript, indexOf() methods provide the first index at which the given element exists, and -1 in case it is not present in the array. Syntax: indexOf(element)indexOf(element, start);Parameters: element: the element to be searched in the input arraystart: the index from which the search has to b
2 min read
How to get the first key name of a JavaScript object ? In JavaScript, accessing the first key name of an object involves identifying the initial property defined within that object. This concept is useful when you need to interact with or manipulate object properties in a specific order, particularly when the sequence of properties is relevant.Here we h
2 min read
JavaScript Property Accessors Method Property Accessors allow access with the property name or keys of an object (Reading, Creating, Updating). There are two notations in JavaScript that allows us to access object's properties: Dot NotationBracket Notation [ ] If the object does not find a matching key(or property name or method name),
1 min read
JavaScript typedArray.indexOf() Method The typedArray.indexOf() is an inbuilt function in JavaScript which is used to return the index of the element if found in the given typedArray otherwise it returns -1. Syntax: typedarray.indexOf(Element, Index); Parameters: It accepts two parameter which are specified below- Element: It is the elem
2 min read