0

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');

1
  • 3
    .map() expects a return value and you are passing string and not an array Commented Oct 23, 2018 at 8:08

6 Answers 6

1

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'))

Sign up to request clarification or add additional context in comments.

Comments

0

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'));

Comments

0

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'));

Comments

0

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');

Comments

0

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');

Comments

0

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

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.