我正在尝试遍历具有属性和值的对象。此对象是动态创建的。我的问题是动态对象属性是一个包含空格的字符串。但是,Javascript对象属性不能包含空格。如何遍历此对象并转换属性名称以便取出空格?感谢您的帮助,以下是以下数据:
ANI: "4693584648"
Action Type: "IVR"
Brand: "Alpha Max Boost"
CSR Transfer: "No"
Call Date: "05/03/2014"
Call Status: "Complete"
Call Time: "15:59:36"
Customer ID: "114360"
DNIS: "9257324175"
First Name: "Isaac"
ID: "342262"
Last Name: "Torres"
OCO Action: "Early Cancel Save Sale Accepted (38.71)"
Order ID: "661438"
Recognition Method: "Automatic"
Status Group: "In Trial - Introduction (38.71)"
答案 0 :(得分:1)
对象在键中可以有空格,如果你还想删除它们,你可以这样做
for (var k in o) {
if (k.replace(/\s/g, '') != k && o.hasOwnProperty(k)) {
o[k.replace(/\s/g, '')] = o[k];
delete o[k];
}
}
如果您有嵌套对象和数组,则必须使其递归
答案 1 :(得分:0)
JavaScript对象当然可以在其属性名称中包含空格。您必须更改setter / getter表示法:
// An array of your sample objects
var test = [{ ... }, { ... }, ...];
// Output the call status of the first one
console.log(test[0]['Call Status']); // should output "Complete"