我正在使用Shopify應用程序,並且我需要進入Mongo的部分訂單信息將作爲通過其API包含單個字符串的屬性發布。舉個例子:Javascript將字符串拆分爲多個對象屬性
"note": "Child 1 First Name: Ali\nChild 1 Gender: Female\nChild 1 Hair Color: Blonde\nChild 1 Hair Style: Wavy\nChild 1 Skin Tone: Tan\nChild 2 First Name: Morgan \nChild 2 Gender: Female\nChild 2 Hair Color: Brown\nChild 2 Hair Style: Ponytail\nChild 2 Skin Tone: Light\nRelationship 1 to 2: Brother\nRelationship 2 to 1: Brother\n",
我真的需要這個字符串看起來像這樣在蒙戈:
mongoExDoc: {
child1FirstName: "Ali",
child1Gender: "Female",
child1HairColor: "Blonde",
child1HairStyle: "Wavy",
child1SkinTone: "Tan",
child2FirstName: "Morgan",
child2Gender: "Female",
child2HairColor: "Brown",
child2HairStyle: "Ponytail",
child2SkinTone: "Light",
relationship1To2: "Brother",
relationship2To1: "Brother"
}
或者沿着這些路線的東西。屬性值本身不會改變。正如你所看到的,每個值由\ n隔開,每個實際值前面都有一個:.我會很感激的建議!
讓我們知道您是否嘗試過 – sidgate
string.split( '\ n')將得到線。 split('').join('')將刪除空格。 +將允許你連接花括號 – danh