在另一个数组javascript中找到数组元素的索引。

时间:2021-10-23 12:36:45

I've 2 1-D arrays like this:

我有2个这样的一维数组:

var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];

I want to find out the index of an element taken from the 'sources' array in the 'words' array. For example, the element 'Helping' in the 'sources' array is located at index 2 in 'words' array. So I need "2" as my solution when I search for index of the word "Helping". Can somebody help me on this? Thanks.

我想找出从“words”数组中的“source”数组中获取的元素的索引。例如,“源”数组中的“帮助”元素位于“单词”数组中的索引2。所以当我搜索“帮助”一词的索引时,我需要用“2”作为我的解。有人能帮我一下吗?谢谢。

I tried using 'indexOf' function in arrays but id didn't work and when I searched for similar questions, it's been said that it'll not work in this problem.

我尝试在数组中使用“indexOf”函数,但是id不起作用,当我搜索类似的问题时,有人说它在这个问题中不起作用。

4 个解决方案

#1


3  

Yes, indexOf will do the job. Check:

是的,indexOf将做这项工作。检查:

var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];

sources.forEach(function(source){
console.log(source, 'is at position', words.indexOf(source));
});

var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];

sources.forEach(function(source){
document.getElementsByTagName('div')[0].innerHTML += source + ' is at position: ' + words.indexOf(source) + '<br>';
});
<div></div>

#2


2  

sources.map(function(word) { return words.indexOf(word); })
// => [2, 7, 1, 4, 5]

#3


0  

You can use an each function.

您可以使用每个函数。

$.each(words, function(key, value) {
      if(value == 'Helping'){
          alert(key);
      }
});

#4


0  

var searchArray=function(value,words){
var result=-1;
for(var i=0;i<words.length;i++){
if(words[i]==value){
result=i;break
}
return result;
}

Answered From mobile. Feel free to format.

从移动回答说。随意的格式。

#1


3  

Yes, indexOf will do the job. Check:

是的,indexOf将做这项工作。检查:

var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];

sources.forEach(function(source){
console.log(source, 'is at position', words.indexOf(source));
});

var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];

sources.forEach(function(source){
document.getElementsByTagName('div')[0].innerHTML += source + ' is at position: ' + words.indexOf(source) + '<br>';
});
<div></div>

#2


2  

sources.map(function(word) { return words.indexOf(word); })
// => [2, 7, 1, 4, 5]

#3


0  

You can use an each function.

您可以使用每个函数。

$.each(words, function(key, value) {
      if(value == 'Helping'){
          alert(key);
      }
});

#4


0  

var searchArray=function(value,words){
var result=-1;
for(var i=0;i<words.length;i++){
if(words[i]==value){
result=i;break
}
return result;
}

Answered From mobile. Feel free to format.

从移动回答说。随意的格式。