0

I have the following array:

["cat", "dog", "cow"] 

I want to replace the values of each with a new string.

["tiger", "wolf", "diary"]

Currently I am traversing through a for loop and checking to see if a string that needs its name to be changed exists, if it does then I am replacing it with the new value. While a for loop works, I am wondering if there's a nicer way of doing this.

2 Answers 2

3

Assuming you have an object with the replacing to do, you can use Array.map

var replace = {
  'cat': 'tiger',
  'dog': 'wolf',
  'cow': 'diary',
};

var starter = ["cat", "dog", "cow"];

var final = starter.map(value => replace[value] || value);
 
console.log(final)

If the string is not in the replace object, replace[value] is undefined, so replace[value] || value is evaluet do value itself.

Anyway, the for is definitely more performing, at least on node.js, accordingly to benchmark.js:

Array.map x 2,818,799 ops/sec ±1.90% (76 runs sampled)
for array x 9,549,635 ops/sec ±1.86% (79 runs sampled)
Fastest is for array

Here the code I used for the test

var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;

suite
.add('Array.map', function() {
  var replace = {
    'cat': 'tiger',
    'dog': 'wolf',
    'cow': 'diary',
  };

  var starter = ["cat", "dog", "cow"];

  var final = starter.map(value => replace[value] || value);
})
.add('for array', function() {
  var replace = {
    'cat': 'tiger',
    'dog': 'wolf',
    'cow': 'diary',
  };

  var starter = ["cat", "dog", "cow"];

  var final = [];

  for (var i = 0; i < starter.length; i++) {
    final.push(replace[starter[i]] || starter[i]);
  }
})
// add listeners
.on('cycle', function(event) {
  console.log(String(event.target));
})
.on('complete', function() {
  console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
Sign up to request clarification or add additional context in comments.

3 Comments

@lost9123193 I did some tries, and the for is more performing (see the updated answer) at least on how I implemented it
ah I see, I probably will not have more than 300 possible entries, but in terms of scalability would you suggest I stick to the for loop?
Well, this is definitely premature optimization and it depends also a lot on how much you need performances. We are still talking of 2 millions operations/sec! I use a lot map() and filter() in my code, because is definitely better looking than a for - but I do not need incredibly performance. I'm quite sure map() is good enough in your case too. I only wanted to show you the entire situation :)
1
function transform(str){
   //logic to transform goes here
}

var massaged = myArr.map(transform);

Maybe thats what youre asking for?

map reference here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

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.