2016-12-24 70 views
1

我有此代碼來查找匹配的名稱並將它們更改爲其他行。 但他們每次都在同一行中改變。JavaScript更改行中的元素並將它們打印在不同的行中

var names1 = ["Rui", "Jose", "Carlos", "Miguel"]; 

var names2 = ["Rui", "Tiago", "Jose", "Carlos", "Rogerio"]; 

for (var i = 0; i < names1.length; i++) { 
    for (var j = 0; j < names2.length; j++) { 

    if (names1[i] == names2[j]) { 

     names2[j] = "Maria", "Fred"; 

    } 

    } 
} 

console.log(names2); 

我想知道如何在不同的行中打印它們。 因爲這樣表示:

MariaFred

蒂亞戈

MariaFred

MariaFred

羅傑里奧

而我想:

瑪麗亞,

弗雷德,

蒂亞戈,

羅傑里奧。

你能幫我嗎?

+1

什麼是預期'names2 [J]的結果= 「瑪麗亞」 被選擇, 「弗雷德」;'? – guest271314

+0

is names2 [j] =「Maria」,「Fred」;一個錯字?它應該是names2 [j] =「Maria」+「Fred」; –

+0

所以我想是要表明這樣的: – Ocirederf

回答

0

你可以使用的excahnge名稱的數組和索引插入更改的NA我如果找到匹配的名字。

var names1 = ["Rui", "Jose", "Carlos", "Miguel"], 
 
    names2 = ["Rui", "Tiago", "Jose", "Carlos", "Rogerio"], 
 
    matched = ["Maria", "Fred"], 
 
    index = 0, 
 
    i, j; 
 

 
outer: for (j = 0; j < names2.length; j++) { 
 
    for (i = 0; i < names1.length; i++) { 
 
     if (names1[i] == names2[j]) { 
 
      names2[j] = matched[index++]; 
 
      index %= matched.length; 
 
      continue outer; 
 
     } 
 
    } 
 
} 
 

 
console.log(names2);

爲了加快檢查,你可以使用一個哈希表和對證。

var names1 = ["Rui", "Jose", "Carlos", "Miguel"], 
 
    names2 = ["Rui", "Tiago", "Jose", "Carlos", "Rogerio"], 
 
    matched = ["Maria", "Fred"], 
 
    index = 0, 
 
    hash = Object.create(null); 
 

 
names1.forEach(function (a) { 
 
    hash[a] = true; 
 
}); 
 

 
names2.forEach(function (a, i, aa) { 
 
    if (hash[a]) { 
 
     aa[i] = matched[index++]; 
 
     index %= matched.length; 
 
    } 
 
}); 
 

 
console.log(names2);

+0

它工作得很好,但在第二次打印再次打印瑪麗亞,任何方式來改變?我得到這個:'瑪麗亞','蒂亞戈','弗雷德','瑪麗亞','羅傑里奧' – Ocirederf

+1

好吧,我只需要添加相同數量的matchin名稱,sry – Ocirederf

0

代替

console.log(names2) 

試試這個

for (var i = 0; i < names2.length; i++) { 
console.log(names2[i]); 

}

+0

不是真的打印瑪麗亞多次,並沒有打印弗雷德。 – Ocirederf

+0

,因爲它printes瑪麗亞元素匹配的次數,並沒有打印第二個名字 – Ocirederf

0

試試這個:

var names1 = ["Rui", "Jose", "Carlos", "Miguel"]; 
var names2 = ["Rui", "Tiago", "Jose", "Carlos", "Rogerio"]; 
var matchFound = false; 
for (var i = 0; i < names1.length; i++) { 
    for (var j = 0; j < names2.length; j++) { 
    if (names1[i] == names2[j] && matchFound === false) { 
     // when the name matches, print Maria and Fred in 2 different lines 
     console.log("Maria,"); 
     console.log("Fred,"); 
     matchFound = true; 
    } else { // if name doesn't match, just print the element from 2nd array 
     console.log(names2[j]"); 
    } 

    } 
} 
+0

它沒有工作,因爲它打印匹配名稱 – Ocirederf

0

在下面的例子,我覺得隨機的名字應該從一些源

var names1 = ["Rui", "Jose", "Carlos", "Miguel"]; 
var names2 = ["Rui", "Tiago", "Jose", "Carlos", "Rogerio"]; 
var random_names = ['Maria', 'Fred', 'Someother'] 
var random_names_counter = 0; 
for (var i = 0; i < names1.length; i++) { 
    for (var j = 0; j < names2.length; j++) { 
     if (names1[i] == names2[j]) { 
      names2[j] = random_names[random_names_counter++]; 
     } 
    } 
} 

console.log(names2); 
相關問題