값으로 배열 인덱스를 찾는 방법은 무엇입니까?
내가 가지고 있다고 말해
imageList = [100,200,300,400,500];
어느 것이 나에게
[0]100 [1]200
기타
JavaScript에서 값으로 색인을 반환하는 방법이 있습니까?
즉 , 200 의 인덱스를 원하면 1 이 반환 됩니다.
당신은 사용할 수 있습니다 indexOf
:
var imageList = [100,200,300,400,500];
var index = imageList.indexOf(200); // 1
배열에서 값을 찾을 수 없으면 -1을 얻습니다.
들어는 배열 객체 를 사용 map
하여 indexOf
:
var imageList = [
{value: 100},
{value: 200},
{value: 300},
{value: 400},
{value: 500}
];
var index = imageList.map(function (img) { return img.value; }).indexOf(200);
console.log(index);
최신 브라우저에서는 다음을 사용할 수 있습니다 findIndex
.
var imageList = [
{value: 100},
{value: 200},
{value: 300},
{value: 400},
{value: 500}
];
var index = imageList.findIndex(img => img.value === 200);
console.log(index);
그 ES6의 일부를 지원하는 크롬, FF, 사파리 및 Edge
jQuery의 함수 jQuery.inArray 사용
jQuery.inArray( value, array [, fromIndex ] )
(or) $.inArray( value, array [, fromIndex ] )
다음은 자바 스크립트의 복잡한 배열에서 값 인덱스를 찾는 다른 방법입니다. 누군가를 도와주세요. 다음과 같이 JavaScript 배열이 있다고 가정 해 봅시다.
var studentsArray =
[
{
"rollnumber": 1,
"name": "dj",
"subject": "physics"
},
{
"rollnumber": 2,
"name": "tanmay",
"subject": "biology"
},
{
"rollnumber": 3,
"name": "amit",
"subject": "chemistry"
},
];
Now if we have a requirement to select a particular object in the array. Let us assume that we want to find index of student with name Tanmay.
We can do that by iterating through the array and comparing value at the given key.
function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}
You can use the function to find index of a particular element as below,
var index = functiontofindIndexByKeyValue(studentsArray, "name", "tanmay");
alert(index);
Use indexOf
imageList.indexOf(200)
how about indexOf
?
alert(imageList.indexOf(200));
Array.indexOf
doesnt work in some versions of internet explorer - there are lots of alternative ways of doing it though ... see this question / answer : How do I check if an array includes an object in JavaScript?
When the lists aren't extremely long, this is the best way I know:
function getIndex(val) {
for (var i = 0; i < imageList.length; i++) {
if (imageList[i] === val) {
return i;
}
}
}
var imageList = [100, 200, 300, 400, 500];
var index = getIndex(200);
It is possible to use a ES6
function Array.prototype.findIndex
.
The
findIndex()
method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.
var fooArray = [5, 10, 15, 20, 25];
console.log(fooArray.findIndex(num=> { return num > 5; }));
// expected output: 1
Find an index by object property.
To find an index by object property:
yourArray.findIndex(obj => obj['propertyName'] === yourValue)
For example, there is a such array:
let someArray = [
{ property: 'OutDate' },
{ property: 'BeginDate'},
{ property: 'CarNumber' },
{ property: 'FirstName'}
];
Then, code to find an index of necessary property looks like that:
let carIndex = someArray.findIndex( filterCarObj=>
filterCarObj['property'] === 'CarNumber');
// Instead Of
var index = arr.indexOf(200)
// Use
var index = arr.includes(200);
Please Note: Includes function is a simple instance method on the array and helps to easily find if an item is in the array(including NaN unlike indexOf)
Here is my take on it, seems like most peoples solutions don't check if the item exists and it removes random values if it does not exist.
First check if the element exists by looking for it's index. If it does exist, remove it by its index using the splice method
elementPosition = array.indexOf(value);
if(elementPosition != -1) {
array.splice(elementPosition, 1);
}
참고URL : https://stackoverflow.com/questions/7346827/how-to-find-the-array-index-with-a-value
'Programing' 카테고리의 다른 글
static_cast <>와 C 스타일 캐스팅의 차이점은 무엇입니까? (0) | 2020.05.16 |
---|---|
Lodash는 배열에서 중복 제거 (0) | 2020.05.16 |
어떤 기본의 정수를 문자열로 변환하는 방법? (0) | 2020.05.16 |
linq 결과를 HashSet 또는 HashedSet로 변환하는 방법 (0) | 2020.05.16 |
사람들은 왜 루비가 느리다고 말합니까? (0) | 2020.05.16 |