我正在使用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