2016-08-20 26 views
-2

我想在每個循環的一個數組中插入兩個值。我曾嘗試如何在JavaScript中一次插入兩個值到數組使用JavaScript

feature_arr = []; 
    $form.find('.variations .value').each(function() { 
         var $radios = $(this).find('input[type=radio]'); 
         var $checked_radio = $radios.filter(':checked'); 
         var attribute_name = $radios.attr('name'); 
         feature_arr[] = attribute_name1; 
         feature_arr[] = $checked_radio.val(); 
        }); 

我想陣這種形式["1", "Brighton_Black", "2", "Frame_Base", "3", "Matching_upholstery", "6", "Headrest", "7", "Covered"]

但請給我錯誤此代碼

+0

你得到什麼錯誤? –

+0

未捕獲的SyntaxError:意外的代幣] @RobertColumbia – Coder

回答

1

試試這個

var feature_arr = new Array(); // define the array or you can use feature_arr = [] 

和您的病情裏面

feature_arr.push(your_value_1); 
feature_arr.push(your_value_2); 

第二部分在評論你的問題試試這個

<html> 
 
<head></head> 
 
<title></title> 
 
<style type="text/css"> 
 

 

 
</style> 
 

 
<body> 
 

 

 

 

 
</body> 
 

 
<script type="text/javascript"> 
 

 
var thestring = "matching_upholstery";// this is your string 
 
alert(toupper(thestring)); // in here we pass out string to the method/function we wrote that returns the converted output string. to see the result we use an alert here. 
 

 

 
//this is the function that convert the first letter to upper and return the final result 
 
function toupper(a) 
 
{ 
 
    var stringArray = a.split('_');//split your word from '_' character. 
 

 
    var firstword = stringArray[0];//get the first word 
 
    var firstwordWithupper = firstword.charAt(0).toUpperCase()+firstword.slice(1);//convert the first letter to upper 
 

 
    var secondword = stringArray[1]; // get the second word 
 
    var secondwordWithupper = secondword.charAt(0).toUpperCase()+secondword.slice(1);//convert the first letter of second word to upper 
 

 
    var finalresult = firstwordWithupper +"_"+secondwordWithupper; 
 
    return finalresult; 
 
    
 
} 
 

 

 
</script> 
 

 
</html>

+0

你是一個絕對的明星。謝謝。 – Coder

+2

歡迎你 –

+0

是的,我可以,你可以把這個作爲一個單獨的問題,讓我知道。我會 –

1

你也可以在一行中做這樣

var a = [].concat(1,2); 

。你的情況的條件裏面,你可以像

feature_arr = feature_arr.concat(value1,value2) 

,或者如果你收到一個陣列的多個數據,這將導致相同

feature_arr = feature_arr.concat([value1,value2]) 
+0

matching_upholstery我有這個詞,我想轉換大寫,如Matching_Upholstery請 – Coder

+0

你可以做'var s =「matching_upholstery」.split(「 「).map(s => s [0] .toUpperCase()+ s.slice(1))。join(」_「);' – Redu

+0

你是一個絕對的傳說。謝謝@redu – Coder

相關問題