0

I'm trying to replace links from a dynamic feed after the results are displayed on the page using JQuery.

Basically, the idea is to look for the values of ARRAY1 and replace them with the same index value of ARRAY2. For example:

ARRAY1 = ['hello','how','are','you'];
ARRAY2 = ['hi'   , 'I' ,'am' ,'ok'];

So, if the loop finds the VALUE 'hello', then replace it for 'hi', if it finds 'how' then replace it with 'I'... and so on.

7
  • so u mean to say that all the values in array1 should be replaces with array2? Commented Dec 4, 2015 at 11:02
  • indexOf() will help you to solve this problem Commented Dec 4, 2015 at 11:03
  • Hi, only if they are found... and they should be replaced with the equivalent index value from the array2, if for example array1[3] doesn't exist, the loop should skip it, and when it finds array1[4] should replace it with the value of array2[4] Commented Dec 4, 2015 at 11:05
  • how it possible? so you have some elements as undefined? Commented Dec 4, 2015 at 11:06
  • the idea is to replace links from an instagram feed, to make only some items go to a different link that won't be the link to the instagram post, and the array1 will be created manually, so because the feed itself will be limited to certain amount of items, and some items in between we won't want to replace the link, the some values should be skipped.... Commented Dec 4, 2015 at 11:09

6 Answers 6

1

You can simply use replace for every item in your array.

var arr1 = ['hello','how','are','you'];
var arr2 = ['hi'   , 'I' ,'am' ,'ok'];

var str = "hello, my friend. how are you doing? Let me say it again: hello, hello, hello";

for (var i = 0; i < arr1.length; i++)
{
  while (str.indexOf(arr1[i]) > -1) 
    str = str.replace(arr1[i], arr2[i]);
}

document.body.innerText = str;

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

1 Comment

It will make more sense to use object (map in this case) of "mappings" instead of the arr1 and arr2. You won't need to have all of the logic inside for loop. Eg. mappings['hello'] should return 'hi'.
1

Try something like

var result = ARRAY1.map(function (dt, i) {
  return ARRAY2[i]
});

This way you don't have to bother if array2 and array1 have different lengths. :)

Comments

0

For each word in your loop:

var index = ARRAY1.indexOf(word);
if (index >= 0) {
    word = ARRAY2[index];
}

Caching the results of indexOf may help with efficiency, depending on the scale of your inputs.

Comments

0
ARRAY1 = ['hello','how','are','you'];
ARRAY2 = ['hi'   , 'I' ,'am' ,'ok'];
value = 'hello';
if(ARRAY2[ARRAY1.indexOf(value)]>-1){
   console.log(ARRAY2[ARRAY1.indexOf(value)]);
}

Comments

0

Try this if you want to replace all

for(var i = 0; i< ARRAY2.lenght; i++){
ARRAY1[i] = ARRAY2[i];

//or
ARRAY2[i] = ARRAY1[i];
}

Comments

0

Try this.

var ARRAY1 = ['hello','how','are','you'];
var ARRAY2 = ['hi'   , 'I' ,'am' ,'ok'];
var replaceValue = function(wordToReplace){
     var index = ARRAY1.indexOf(wordToReplace);
     ARRAY1[index] = ARRAY2[index];
}
replaceValue("hello");

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.