0

I want to make a function that has two arrays in it. The first array contains numbers and the 2nd contains letters and numbers. I want to compare them, and if a certain element within the 2nd array is also on the 1st array, then to replace the element of the 1st array to something else. This is the example array i've written:

const array1 = ['1', '2', '3', '4', '5']
const array2 = ['a', 'b' '3', '2', 'c']

So the 2nd array and the 1st array both have numbers 3 and 2. This is my code for trying to compare the values:

const arraycompare = array1.some(x => array2.includes(x))
const finalarray = array1.replace(arraycompare, `${arraycompare} - also found in array2`
console.log(finalarray)

However this doesn't work because arraycompare returns a boolean and not the value of the elements that it had compared and found that they are present in both arrays. So how am I supposted to fix this and make it so that arraycompare and finalarray work?

4 Answers 4

2

Use Array.map:

const array1 = ['1', '2', '3', '4', '5']
const array2 = ['a', 'b' ,'3', '2', 'c']

let n = array1.map(e => array2.includes(e)?'replacement':e)

console.log(n)
Sign up to request clarification or add additional context in comments.

Comments

1

You don't necessarely need to create an intermediate array. You could just #map array1: for each element look if it's also in array2, if so return your string else return the value. I'm using the ternary operator for that:

array2.includes(a1) ? `${a1} - also found in array2` : a1

Here's the code:

const array1 = ['1', '2', '3', '4', '5']
const array2 = ['a', 'b', '3', '2', 'c']

console.log(
  array1.map(a1 => array2.includes(a1) ? `${a1} - also found in array2` : a1)
)

Comments

0

This may not be the cleanest way but I think it has value so you can see the logic behind it. First you should loop through both arrays comparing each element in the first with each in the second. Set a bool to false before looping through the second array, upon matching set it to true. When the second array is finished iterating over check if the bool was changed(the element matched). If it did you know that element in the first array is in the second so you can then set that element to be whatever you would like.

let array1 = ['1', '2', '3', '4', '5']
const array2 = ['a', 'b', '3', '2', 'c']

array1.forEach((a1Element, index) =>{
  let found = false;
  array2.forEach((a2Element) => {
    if(a1Element === a2Element){
      found = true;
    }
  })
  
  if(found){
      array1[index] = `${a1Element} also found in array2`;
  }
})

console.log(array1)

Comments

0
const array1 = ['1', '2', '3', '4', '5']
const array2 = ['a', 'b', '3', '2', 'c']


array1.forEach((hash => (a, i, aa) => {
  var exists = array2.find(f => f === a);
  if (exists) {
    aa[i] = "replaced " + exists
  }
})(new Set(array1)));

console.log(array1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.