0

I currently have this problem where I cannot seem to find an answer specific to my problem.

Array1 = ["item1", "item2", "item3", "item4", "item5"]
Array2 = ["item2", "item5"]

I am looking to use the information from array2 to find within array1.

An example for this to output

Array1 has item2 and is at Array1[1]

If anyone can help me, thank you.

  • Why doesn't your example output mention `"item5"`? Can the same string potentially occur more than once in `Array1`? What have you tried so far? I'm thinking a simple `for` loop over `Array2` with a call to `Array1.indexOf()` would get the job done easily enough. If your question is "Please code the whole thing for me" what you really want is a JS tutorial - there are links to some on the [SO JS info page](http://stackoverflow.com/tags/javascript/info). – nnnnnn Feb 20 '17 at 01:58
  • What is your question? You merely stated facts about the arrays – m_callens Feb 20 '17 at 02:00

4 Answers4

2
Array2.forEach((e) => {
    const indexOfE = Array1.indexOf(e)
    if (indexOfE > -1) {
        console.log(`Array1 has ${e} and is at Array1[${indexOfE}]`)
    }
})

You can have a look at forEach, indexOf, and template literals to help you understand this code.

Edit

Answering the question in the comments, if you want to check the elements in Array1 that contain elements of Array2 as substrings, then you can:

Array2.forEach((e) => {
    Array1.forEach((f, i) => {
        if (f.toLowerCase().includes(e)) {
            console.log(`Array1 has ${e} and is at Array1[${i}]`)
        }
    })
})

Check String.prototype.includes and this answer for details on finding substrings in another String.

Community
  • 1
  • 1
  • Outstanding answers. Thank you. One question. Would it be possible to have `Array2` search more in terms of find the word specifically regardless of how it is within `Array1`? More like an includes or contains method. For example say it was " largeitem1 " and `Array2` still had "item1" – UndefinedUsername Feb 20 '17 at 02:54
  • @UndefinedUsername yes, check the edit for an answer. – Marcus Vinícius Monteiro Feb 20 '17 at 03:17
0

If you only want to know Array1 contains an element from Array2 then iterate over Array2 calling indexOf for each element:

Array2.map((el => {
    if (Array1.indexOf(el) !== -1) {
        console.log('Array 1 contains ' + el);
    }
}));
Chris Cousins
  • 1,862
  • 8
  • 15
  • 1
    `.forEach()` is a better choice than `.map()`. The result is the same, but `.map()` implies that you are, well, *mapping* something. Which you're not. – nnnnnn Feb 20 '17 at 02:24
  • Yes you're right. I tend to opt for map out of habit - but foreach is better here. – Chris Cousins Feb 20 '17 at 02:27
0

This is the code:

var Array1 = ["item1", "item2", "item3", "item4", "item5"];
var Array2 = ["item2", "item5"];
for (var i = 0; i <Array2.length; i++) {
 if (Array1.indexOf(Array2[i])>-1) {
 console.log("Array1 has "+Array2[i]+" and is at Array1["+Array1.indexOf(Array2[i]) +"]")  
  }
}
shangzhouwan
  • 246
  • 1
  • 6
0

You could Ramdajs to find intersection of two arrays as follows

const Array1 = ["item1", "item2", "item3", "item4", "item5"];
const Array2 = ["item2", "item5"];

const res = R.intersection(Array1, Array2);
console.log(res);

here is the code repel

The same could be achieved using lodash

const Array1 = ["item1", "item2", "item3", "item4", "item5"];
const Array2 = ["item2", "item5"];
const res = _.intersection(Array1, Array2)
console.log(res);

here is the jsfiddle for lodash.

or you could use the includes method of the array to do the same

const Array1 = ["item1", "item2", "item3", "item4", "item5"];
const Array2 = ["item2", "item5"];
const res = Array1.filter(curr=>Array2.includes(curr));
console.log(res);
user93
  • 1,866
  • 5
  • 26
  • 45