jsÖÐjoinµÄÓ÷¨
join() ÒªÁ콫Êý×éÖеÄÔªËØÅþÁ¬³ÉÒ»¸ö×Ö·û´®£¬Ê¹ÓÃÖ¸¶¨µÄ×Ö·û×÷ΪÍÑÀë·û¡£ÏêϸÓ÷¨°üÀ¨£ºÅþÁ¬×Ö·û´®ÔªËØ¡£Ê¹ÓÃ×Ô½ç˵ÍÑÀë·û¡£ÅþÁ¬¹¤¾ßÊý×飨µ«»á»ñµÃÒ»¸ö°üÀ¨¹¤¾ß json ×Ö·û´®µÄ×Ö·û´®£©¡£
JavaScript ÖÐ join() ÒªÁìÏê½â
ʲôÊÇ join() ÒªÁ죿
join() ÒªÁìÓÃÓÚ½«Êý×éÖеÄËùÓÐÔªËØÅþÁ¬ÎªÒ»¸ö×Ö·û´®£¬²¢Ê¹ÓÃÖ¸¶¨µÄ×Ö·û×÷ΪÍÑÀë·û¡£
Óï·¨£º
array.join(separator)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
array£ºÒªÅþÁ¬µÄÊý×é¡£
separator£ºÅþÁ¬ÔªËØʱʹÓõÄÍÑÀë·û¡£Ä¬ÒÔΪ¶ººÅ£¨,£©¡£
Ó÷¨£º
ÅþÁ¬×Ö·û´®ÔªËØ£º
const names = ["John", "Mary", "Bob"]; const nameString = names.join(); // Ч¹û£º"John,Mary,Bob"
µÇ¼ºó¸´ÖÆ
ʹÓÃ×Ô½ç˵ÍÑÀë·û£º
const numbers = [1, 2, 3, 4, 5]; const numberString = numbers.join("-"); // Ч¹û£º"1-2-3-4-5"
µÇ¼ºó¸´ÖÆ
ÅþÁ¬¹¤¾ßÊý×飺
const objects = [ { name: "John", age: 30 }, { name: "Mary", age: 25 }, { name: "Bob", age: 40 }, ]; const objectString = objects.join(); // Ч¹û£º"{"name":"John","age":30},{"name":"Mary","age":25},{"name":"Bob","age":40}"
µÇ¼ºó¸´ÖÆ
×¢ÖØÊÂÏ
ÈôÊÇÊý×éΪ¿Õ£¬join() ÒªÁì·µ»ØÒ»¸ö¿Õ×Ö·û´®¡£
ÍÑÀë·û¿ÉÒÔÊÇÒ»¸ö×Ö·û´®¡¢Êý×Ö»òÈκÎÆäËûÊý¾ÝÀàÐÍ¡£
ÈôÊÇÍÑÀë·ûΪ undefined »ò null£¬ÔªËؽ«±»ÅþÁ¬ÔÚÒ»Æð£¬Ã»ÓÐÍÑÀë·û¡£
ÒÔÉϾÍÊÇjsÖÐjoinµÄÓ÷¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡