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(', ');
returnin the map function.a.indexOf(4) >= 0. I don't see a reason to use.map()here.