我是谷歌應用腳??本的新手,每天都在努力學習。我為我的基本知識道歉。我正在嘗試以特定方式拆分字串。這是陣列中的字串:
var data = [call number="7203266298" duration="0" date="1646769239639" type="2" presentation="1" subscription_id="89148000007344410028" post_dial_digits="" subscription_component_name="com.android.phone/com. android.services.telephony.TelephonyConnectionService" readable_date="2022 年 3 月 8 日下午 12:53:59"contact_name="(Unknown)"]
現在我想按以下格式拆分此文本:
var data = [call number="7203266298",
duration="0",
date="1646769239639",
type="2",
presentation="1",
subscription_id="89148000007344410028",
subscription_component_name="com.android.phone/com.android.services.telephony.TelephonyConnectionService",
readable_date="Mar 8, 2022 12:53:59 PM",
contact_name="(Unknown)"]
我嘗試使用split()這樣的功能:
data = data.split(" ")
但是這種方法的輸出并不是我真正需要的,它會創建像這樣的不必要的磁區:
[ , , call, number=" 12532250046", duration="0", date="1646851016349", type="3", presentation="1", subscription_id="89148000007344410028", post_dial_digits="", subscription_component_name=" com.android.phone/com.android.services.telephony.TelephonyConnectionService", readable_date=" Mar , 9 ,, 2022 , 11:36:56 , AM ", contact_name="(Unknown)", ]
任何指導將不勝感激。
uj5u.com熱心網友回復:
實作此目的的一種方法是將陣列索引組合到您想要組合查看的新陣列中。例如:
var temp = [call number="7203266298" duration="0" date="1646769239639" type="2" presentation="1" subscription_id="89148000007344410028" post_dial_digits="" subscription_component_name="com.android.phone/com.android.services.telephony.TelephonyConnectionService" readable_date="Mar 8, 2022 12:53:59 PM" contact_name="(Unknown)"];
temp = temp.split(" ");
newData = [temp[0] " " temp[1], temp[2],temp[3],temp[4],temp[5],temp[6], temp[7],temp[8],temp[9] " " temp[10] " " temp[11] " " temp[12] " " temp[13],
temp[14]];
Logger.log(newData);
Output:
[call number="7203266298", duration="0", date="1646769239639", type="2", presentation="1", subscription_id="89148000007344410028", post_dial_digits="", subscription_component_name="com.android.phone/com.android.services.telephony.TelephonyConnectionService", readable_date="Mar 8, 2022 12:53:59 PM", contact_name="(Unknown)"]
uj5u.com熱心網友回復:
這是另一個使用簡單正則運算式的解決方案,它應該適用于任何數量的元素,并且如果訂單發生變化:
function myFunction() {
var data = '[call number="7203266298" duration="0" date="1646769239639" type="2" presentation="1" subscription_id="89148000007344410028" post_dial_digits="" subscription_component_name="com.android.phone/com.android.services.telephony.TelephonyConnectionService" readable_date="Mar 8, 2022 12:53:59 PM" contact_name="(Unknown)"]'
var re = new RegExp('"(.*?)"', "g") // finds sequences enclosed in quotes ""
data = data.slice(1,-1) //removes the [] for a cleaner look if necessary
var values = data.match(re)
var indexes = data.replace(re, "/-/").split("/-/").slice(0, -1)
var output = []
for (i=0; i< indexes.length;i ){
output.push(indexes[i].trim() values[i])
}
console.log(output)
}
輸出:
[ 'call number="7203266298"',
'duration="0"',
'date="1646769239639"',
'type="2"',
'presentation="1"',
'subscription_id="89148000007344410028"',
'post_dial_digits=""',
'subscription_component_name="com.android.phone/com.android.services.telephony.TelephonyConnectionService"',
'readable_date="Mar 8, 2022 12:53:59 PM"',
'contact_name="(Unknown)"' ]
它的作業方式是找到所有"values",將它們放入陣列中,并用唯一的分隔符替換它們"/-/",然后使用該分隔符拆分索引,然后構建一個新output陣列,將索引陣列與值陣列連接起來。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/527093.html
