Adam
Adam

Reputation: 5

How to replace instances of element within array?

I am attempting to replace all of the instances of a defined string within my array with another, different defined string. Think, replacing all empty strings with 'n/a' or something similar.

function replaceThis(array, replace, withThis) {    
  const mapped = array.map(i => {
    if (i === replace) {
      array[array.indexOf(i)] = withThis;
    }
  })
  return mapped
}

However, when I run this, I seem to get an array of all undefined items.

Am I missing something?

function x(array, replace, withThis) {
  console.log(array);

  const m = array.map(i => {
    if (i === replace) {
      array[array.indexOf(i)] = withThis;
    }
  })
  console.log(m);
  return m;
}

x('one', '', 'three');

Upvotes: 0

Views: 61

Answers (6)

apokryfos
apokryfos

Reputation: 40673

You can achieve what you want using simple built-ins:

function replaceThis(array, replace, withThis) {
     var i = array.indexOf(replace);
     if (i >= 0) {
        array[i] = withThis;
     }
     return array;
}

This way you get the same array reference as a result

Upvotes: 0

Elisha Senoo
Elisha Senoo

Reputation: 3594

Try this:

function x(array, replace, withThis) {
   console.log(array);

   const m = array.map(i => {
   //remove strict comparison
   if (i == replace) {
     array[array.indexOf(i)] = withThis;
   }
   }, 'N/A')//set 'N/A' as default
   console.log(m);
   return m;
}

x('one', '', 'three');

Upvotes: 0

Pablo Aragon
Pablo Aragon

Reputation: 333

First of all, you are passing the first argument as string, not as an array.

The correct way Will be: x(['one'], '', 'three');

Second one, in map method, you should always return a value. If you don't, the value in the array will be undefined

function x(array, replace, withThis) {
  console.log(array);

  const m = array.map(i => {
    if (i === replace) {
      return withThis;
    }

    return i;
  })
  console.log(m);
  return m;
}

x(['one', 'two', 'three'], 'two', 'three');

Upvotes: 0

Alex G
Alex G

Reputation: 1917

A more concise way to do this could be this, using the ES6 syntax. Since map returns an new array you don't have to replace the value, you can just return the new value.

const x = (array, replace, withThis) => array.map((v) => v === replace ? withThis : v);

console.log(x(['one', '', 'three'], '', 'XXX'));

Upvotes: 0

Nitish Narang
Nitish Narang

Reputation: 4184

You are missing return keyword and one use case (when i != replace). Also when calling x function, you are passing first param as string, and it accepts an array. Updated your attempt below, you can check it

function x(array, replace, withThis) {
  const m = array.map(d => {
    if (d === replace) return withThis;
    return d
  })
  return m;
}

console.log(x(['one', '', ''], '', 'three'))

Upvotes: 1

Artyom Amiryan
Artyom Amiryan

Reputation: 2966

you didn't pass 2 other parameters replace and withThis , also your stings argument was not in array, and final issue was you should return from .map so it can create new array, as you didn't return nothing from .map it returned undefined and you got array of undefined

    function x(array, replace, withThis) {
      const m = array.map(i => {
        if (i === replace) {
          i = withThis;
        }
        return i;
      })
      return m;
    }
    
    console.log(x(['one', '', 'three'], '', 'n/a'));

Upvotes: 0

Related Questions