How to find the array index with a value?
You can use indexOf
:
var imageList = [100,200,300,400,500];
var index = imageList.indexOf(200); // 1
You will get -1 if it cannot find a value in the array.
Python: find position of element in array
Have you thought about using Python list's .index(value)
method? It return the index in the list of where the first instance of the value
passed in is found.
How do I get the index of object in array using angular?
As mdn says:
The findIndex() method returns the index of the first element in the
array that satisfies the provided testing function. Otherwise, it
returns -1, indicating that no element passed the test.
If you have array of objects like this, then try to use findIndex
:
const a = [
{ firstName: "Adam", LastName: "Howard" },
{ firstName: "Ben", LastName: "Skeet" },
{ firstName: "Joseph", LastName: "Skeet" }
];
let index = a.findIndex(x => x.LastName === "Skeet");
console.log(index);
How to find the index of an element in an array in Java?
In this case, you could create e new String from your array of chars and then do an indeoxOf("e") on that String:
System.out.println(new String(list).indexOf("e"));
But in other cases of primitive data types, you'll have to iterate over it.
Find index of number in array Javascript
It's very simple. First apply Math.min method and spread your array and easily get smallest number.Then you can find the index of smallest number in array by indexOf method.
Here is the code:
const rotateNum = array => {
const small = Math.min(...array)
return array.indexOf(small)
}
How to find index of all occurrences of element in array?
The .indexOf()
method has an optional second parameter that specifies the index to start searching from, so you can call it in a loop to find all instances of a particular value:
function getAllIndexes(arr, val) {
var indexes = [], i = -1;
while ((i = arr.indexOf(val, i+1)) != -1){
indexes.push(i);
}
return indexes;
}
var indexes = getAllIndexes(Cars, "Nano");
You don't really make it clear how you want to use the indexes, so my function returns them as an array (or returns an empty array if the value isn't found), but you could do something else with the individual index values inside the loop.
UPDATE: As per VisioN's comment, a simple for loop would get the same job done more efficiently, and it is easier to understand and therefore easier to maintain:
function getAllIndexes(arr, val) {
var indexes = [], i;
for(i = 0; i < arr.length; i++)
if (arr[i] === val)
indexes.push(i);
return indexes;
}
Trying to get the index of a value in an array in JavaScript
Simply use the array.indexOf()
method.
By feeeding the search string(or any search item) as parameter, it will return the index of that item in the array.
P.S: Remember that array indexes start from 0
and end with array length-1
.
//and you're getting the selected valuefunction GetSelectedItem(el) { var e = document.getElementById(el); var city = e.options[e.selectedIndex].text; var cities = ["Paris", "Adelaide", "Texas", "London", "New York"]; var city_index = cities.indexOf(city); console.log(city_index);}
<!--If you have a dropdown list that looks something like:--><select id="cityList"> <option value="Bengaluru">Bengaluru</option> <option value="Dallas" selected="selected">Dallas</option> <option value="Texas">Texas</option></select><button onClick="GetSelectedItem('cityList');">Get Selected Item</button>
Get index of parent given a value of a child array of arrays
Use Array.findIndex
with condition
const data = [
[
{id: "1",contactType: {id: "phoneNumber",company: {id: "01",name: "Company01"}},value: "5555555555"},
],
[
{id: "2",contactType: {id: "phoneNumber",company: {id: "03",name: "Company03"}},value: "7777777777"},
{id: "3",contactType: {id: "phoneNumber",company: {id: "05",name: "Company05"}},value: "8888888888"},
],
];
const index = data.findIndex(item => item.findIndex(node => node.value === "5555555555") > -1);
console.log(index)
How to find index in nested JS array
This will return the index you are looking for:
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
function find(matrix, item) {
return matrix.findIndex(row => row.indexOf(item) !== -1);
}
console.log(find(matrix, 6));
JavaScript - find index of array inside another array
To compare the actual values you can use the JSON.stringify() method:
piece = [5, 10];
array = [[5, 10], [5, 11]];
//Using findIndex
console.log(array.findIndex(function(element) {
return JSON.stringify(element) == JSON.stringify(piece);
}));
Related Topics
Differencebetween Preservereferenceshandling and Referenceloophandling in JSON.Net
Linq to SQL and a Running Total on Ordered Results
How to Hide a Column (Gridview) But Still Access Its Value
Are There Any Suggestions for Developing a C# Coding Standards/Best Practices Document
Mirroring Console Output to a File
Verifying Jwt Signed with the Rs256 Algorithm Using Public Key in C#
X509Certificate Constructor Exception
C# - Realtime Console Output Redirection
C# Console Receive Input with Pipe
Where's the Datetime 'Z' Format Specifier
What Does It Mean for a Property to Be [Required] and Nullable
What Are the Benefits of Using C# VS F# or F# VS C#
No Connection String Named 'Myentities' Could Be Found in the Application Config File