我有一个对象:
var obj = {
'a|a' : 1,
'b|b' : 2
}
我想将obj
更改为:
var obj = {
'aa' : 1,
'bb' : 2
}
其中属性a|a
已更改为aa
。
有没有办法做同样的事情?
答案 0 :(得分:3)
你可以这样做:
Object.getOwnPropertyNames(obj).forEach(function (key) {
obj[key.replace('|', '')] = obj[key];
delete obj[key];
});
只需遍历所有对象键并将值分配给新密钥(不分析|
),然后删除旧密钥。
答案 1 :(得分:2)
var obj = {
'a|a': 1,
'b|b': 2
}
let keys = Object.keys(obj);
let newObj = {};
for (let key of keys) {
let transformedKey = key.replace("|","") ; // transform your key
newObj[transformedKey] = obj[key]
}
console.log(newObj);
这是修复你的用例。
答案 2 :(得分:2)
有许多方法可以迭代Object。我认为最直接的方法是使用function myWindow()
{
visitorEmail = document.getElementById("myEmail").value;
var findSymbol = /[@]/g;
var result = visitorEmail.match(findSymbol);
if (result != "@") {
document.getElementById("myEmail").focus();
}
else {
visitorName = document.getElementById("myName").value;
visitorAddress = document.getElementById("myAddress").value;
visitorPhone = document.getElementById("myPhone").value;
visitorPersonal = document.getElementById("myPersonal").value;
visitorCareer = document.getElementById("myCareer").value;
visitorEdu = document.getElementById("myEdu").value;
visitorEmp1 = document.getElementById("myEmp1").value;
visitorEmpDets1 = document.getElementById("myEmpDets1").value;
visitorEmp2 = document.getElementById("myEmp2").value;
visitorEmpDets2 = document.getElementById("myEmpDets2").value;
visitorEmp3 = document.getElementById("myEmp3").value;
visitorEmpDets3 = document.getElementById("myEmpDets3").value;
visitorEmp4 = document.getElementById("myEmp4").value;
visitorEmpDets4 = document.getElementById("myEmpDets4").value;
visitorRef = document.getElementById("myRef").value;
myTop = ("<html>\n<head>\n<title>Resume</title>\n</head>\n<body>\n");
myTop += (visitorName + "<br>");
myTop += (visitorAddress + "<br>");
myTop += (visitorPhone + "<br>");
myTop += (visitorEmail + "<hr>" + "<br>");
myText = ("<html>\n<head>\n<title>Resume</title>\n</head>\n<body>\n");
myText += (visitorPersonal + "<br>"+ "<br>");
myText += (visitorCareer + "<br>"+ "<br>");
myText += (visitorEdu += "<br>"+ "<br>");
myText += (visitorEmp1 += "<br>");
myText += (visitorEmpDets1 += "<br>"+ "<br>");
myText += (visitorEmp2 += "<br>");
myText += (visitorEmpDets2 += "<br>"+ "<br>");
myText += (visitorEmp3 += "<br>");
myText += (visitorEmpDets3 += "<br>"+ "<br>");
myText += (visitorEmp4 += "<br>");
myText += (visitorEmpDets4 += "<br>"+ "<br>");
myText += (visitorRef += "<br>");
myText += ("</body>\n</html>");
myHeadings = ("<html>\n<body>\n");
myHeadings += ("PERSONAL INFORMATION"+ "<br>");
myHeadings += ("CAREER OBJECTIVES"+ "<br>");
myHeadings += ("EDUCATIONAL BACKGROUND"+ "<br>");
myHeadings += ("EXPERIENCE"+ "<br>");
myHeadings += ("BUSINESS REFERENCES"+ "<br>");
flyWindow = window.open('about:blank','myPop','width=600,height=500,left=200,top=200');
flyWindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="jsStyles.css" /></head><body><div>' + myTop + myText + '</div></body></html>');
}
}
语句:
for..in
因此,更改密钥名称将涉及使用for (let key in obj) {
console.log(key, '=>', obj[key]);
}
更改密钥名称:
String.replace
&#13;
如果您不想创建新对象,可以添加新密钥和var obj = {
'a|a' : 1,
'b|b' : 2
}
let newObj = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key.replace('|', '')] = obj[key];
}
}
console.log(newObj);
delete obj[key]
另一种方法是在键/属性上使用for (let key in obj) {
if (obj.hasOwnProperty(key)) {
obj[key.replace('|', '')] = obj[key];
delete obj[key];
}
}
:
Array.reduce