0

The definition in my book is the method passes each element of the array on which it is invoked to the function you specify, and returns a new array containing the value returned by that function.

a = [1,2,3]
a.map(function(x) { return x*x; }); // b is [1,4,9]

I would want the function to only return 1 if 4 is not found.

The case would be

var bool = false;
a.map(function(x) {

if (x == 4){
  bool = true;
}

return x;
}).filter(function(x) {if( (x == 1) && ( bool = true)){ return null}});

The way I would like to use it is by iterating over an array and than dynamically change the map at the end. How would I do that?

My problem now is with strings, so Here is another case, where 1 is now called unknown. And if anything after "unknown" is found, remove "unknown" from the list before joining.

 var wordList = [];
    var newSource = false;
    str = results[i].Meaning.map(function(m){
        count++;

        if ((!m.Source && !contains(wordList, "unknown"))) {
            wordList[count] = "unknown";
            return "unknown";
            }
        if (!m.Source ) {
            return m.Source;
        }

            if ( contains(wordList, "unknown") ) {
                newSource = true;
            }
            if (!contains(wordList, m.Source) ) {
                wordList[count] = m.Source;
                return m.Source;
            }

    }).filter(function(x) { return x  }).filter(function(x){
        if (newSource == true ) { return (x != "unknown")}}).join(', ');
8
  • You didn't include a return in the map function. Commented Feb 11, 2015 at 16:43
  • For questions like that I suggest to always take a look at MDN Docs. Commented Feb 11, 2015 at 16:44
  • It sounds like you just want a.indexOf(4) >= 0. I don't see a reason to use .map() here. Commented Feb 11, 2015 at 16:53
  • That is a general case, usually there will be many conditions inside the map. The one that is being annoying is dynamically changing the contents. Commented Feb 11, 2015 at 16:55
  • It is just not clear what problem you're trying to solve. You have an input array, but it is not clear what you want the final output to be. Please show both input and desired output and I'm sure folks here can help you select the best way to get there, but you have to show the desired result. Commented Feb 11, 2015 at 16:56

1 Answer 1

2

Let's look at the first function:

function f1(x) {
  var bool = false;

  if (x == 4){
    bool = true; 
  }

  return x;
}

This function changes the variable bool locally, and returns x. So, no matter what happens to bool, this function is equivalent to the identity function:

function(x) { return x; }

Because .map(f) returns an array with f applied to all elements, we have that a.map(f1) is equivalent to a.map(identity function) which is equivalent to a.

The second function is inside the filter:

if( (x == 1) && ( bool = true)) return null;

We have some issues here:

  • There's no function(x) signature
  • You are trying to access the bool variable, that was declared on the first function.

I suggest whenever you use map and filter, that you use pure functions, which means your function just process the parameter passed to them, and return the result.

I'm not sure what you are trying to accomplish in the first problem; please give more details and I'll try to help you with a solution.

Look for tutorials in map, filter and reduce on Google. For example, this egghead video.

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

3 Comments

Thank you for the help and link. I fixed the edits by the way.
Much better now, @collegeWaldo . But, I'm still having a problem understanding what you want with your second algorithm. Could you please give some examples of inputs and outputs on your question, so I could help you with a solution?
Yes will reply within 24 hours.

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.