我遇到的问题是API给了一个长字符串,其中包含需要分解为对象的多个项目。以下是字符串的示例:
"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"
正在使用\n
将所有内容分成多行。该字符串是API发送的较大对象的一个属性。出于某种原因,当我尝试使用.split
方法时(如下面的完整代码),我需要使用JSON.stringify
才能让我的功能开始工作。
这是我的完整代码:
let rawNoteData = order.customer.note;
let data = JSON.stringify(rawNoteData);
let foo = data.split("\n").reduce(function(obj, str, index) {
let strParts = str.split(":");
obj[strParts[0].replace(/\s+/g, '')] = strParts[1];
return obj;
}, {});
console.log(foo);

这会创建看起来很疯狂的对象:
{'"Child1FirstName': ' Arabelle\\nChild 1 Gender' }

我认为我的功能仅适用于第一个实例。我有点不确定如何解决这个问题,并清理所有疯狂的报价等等。
我的目标是将字符串拆分为如下对象:
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"
}

从那里我将把它的属性与插入到MongoDB中的另一个对象相结合。
非常感谢任何帮助!
答案 0 :(得分:9)
如果order.customer.note
是您的示例字符串,那么这应该有效:
let data = "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";
//let data = JSON.stringify(rawNoteData); <-- Don't do this. order.customer.note is not an object.
let foo = data.split("\n").reduce(function(obj, str, index) {
let strParts = str.split(":");
if (strParts[0] && strParts[1]) { //<-- Make sure the key & value are not undefined
obj[strParts[0].replace(/\s+/g, '')] = strParts[1].trim(); //<-- Get rid of extra spaces at beginning of value strings
}
return obj;
}, {});
console.log(foo);
&#13;
答案 1 :(得分:0)
我可以想到将字符串转换为对象的两种方法:
JSON#parse
解析。
const str = "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";
const toObject = (str) => {
const json = str.replace(/([^\:]+)\:([^\n]+)\n/g, (_, p1, p2) => {
return `"${p1.replace(/\s+/g, '')}":"${p2.trim()}",`;
});
return JSON.parse(`{${json.slice(0, -1)}}`);
};
console.log(toObject(str));
或强>
const str = "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";
const toObject = (str) => {
const arr = str.match(/[^\:\n]+/g);
const obj = {};
for(let i = 0; i < arr.length; i += 2) {
obj[arr[i].replace(/\s+/g, '')] = arr[i + 1].trim();
}
return obj;
};
console.log(toObject(str));
答案 2 :(得分:0)
这样的东西?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour {
public float moveSpeed;
// Use this for initialization
void Start ()
{
moveSpeed = 10f;
}
// Update is called once per frame
void Update ()
{
transform.Translate(moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f,moveSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
}
}
生成一个像你要求的对象