如何替换空白并从对象的所有键中删除特殊字符



我有以下数组对象-在其中,我想用下划线(_(标记替换所有空白,并删除forEach循环中对象所有键中的所有特殊字符。

阵列输入:

[{
"Location Number": 49,
"Location Full Name": "New York",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
},
{
"Location Number": 44,
"Location Full Name": "New Jersey",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
}]

预期输出:

[{
"Location_Number": 49,
"Location_Full_Name": "New York",
"Location_Status": "OPEN",
"RegionState": "NY",
"Postal_Code": 10010,
"Country": "USA",
"Location_Email_Address": "email@domain.com",
"Location_Telephone_Number": "(123) 233 4453",
"Node_Type": "Retail Store",
"User_Login_Location": "Yes"
},
{
"Location_Number": 44,
"Location_Full_Name": "New Jersey",
"Location_Status": "OPEN",
"RegionState": "NY",
"Postal_Code": 10010,
"Country": "USA",
"Location_Email_Address": "email@domain.com",
"Location_Telephone_Number": "(123) 233 4453",
"Node_Type": "Retail Store",
"User_Login_Location": "Yes"
}]

JavaScript:

inputArray.forEach(function (item, index, object) {
const key = Object.keys(item);
console.log(key);
key.forEach(function(){
// somthing
});
});

按顺序;"干净";需要使用2个regexs:的密钥

  1. 将空白字符替换为_.replace(/s+/g, '_')
  2. .replace(/W+/g, '')将不将字母数字字符替换为''

const input = [{
"Location Number": 49,
"Location Full Name": "New York",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
},
{
"Location Number": 44,
"Location Full Name": "New Jersey",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
}
];
function changeKeys(item, index) {
return Object.keys(item).reduce((result, key) => {
const cleanKey = key.replace(/s+/g, '_').replace(/W+/g, '')
result[cleanKey] = input[index][key];
return result;
}, {});
}
const result = input.map(changeKeys)
console.log(result);

Ciao,您可以使用splitjoin函数将" "替换为"_",并使用replace删除特殊字符,如:

let input = [{
"Location Number": 49,
"Location Full Name": "New York",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
},
{
"Location Number": 44,
"Location Full Name": "New Jersey",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
}]
let result = input.map(el => {
let resultobj = {};
Object.entries(el).map(([key, val]) => {      
resultobj[key.split(" ").join("_").replace(/W+/g, '')] = val;
return resultobj;
} )
return resultobj;
})
console.log(result)

您所要求的可以实现转换为元组,转换键,然后重新创建每个对象(使用object.entries和object.fromEntries方法(:

const rmWhite = o => Object.fromEntries(Object.entries(o).map(([k,v]) => [k.replace(/ /g,  '_'), v]))
const res = inputArr.map(rmWhite)

在这个例子中,我只是用_替换空白,如果你想要其他操作,你需要在map回调中添加它们。


编辑:在下面,您可以只使用一个正则表达式(在正则表达式回调中切换大小写(找到解决方案的完整片段:

const inputArr = [{
"Location Number": 49,
"Location Full Name": "New York",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
},
{
"Location Number": 44,
"Location Full Name": "New Jersey",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
}
];
const subsChars = o => Object.fromEntries(Object.entries(o).map(
([k, v]) => [k.replace(/[ W]/g, m => m === ' ' ? '_' : ''), v]
))
const res = inputArr.map(subsChars)
console.log(res)

您可以通过将Array#MapArray#ForEachreplace一起使用,并使用一个简单的Regex将所有spaces替换为underscore来实现这一点。

编辑:刚刚注意到您希望/也从Region/State中删除-我也添加了ReGex,以匹配确切的预期输出。

现场演示:

var inputArray = [{
"Location Number": 49,
"Location Full Name": "New York",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
},
{
"Location Number": 44,
"Location Full Name": "New Jersey",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
}
]
var newArray = []
inputArray.map(function(item, index, object) {
const result = {}; //store new entries
Object.keys(item).forEach(function(x) {
result[x.replace(/ +/g, '_').replace(/W+/g, '')] = item[x]; //replace space with _
});
newArray.push(result) //push new results
});
console.log(newArray) //new Array

这很简洁,正是您所需要的:

const arr = [{
"Location Number": 49,
"Location Full Name": "New York",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
},
{
"Location Number": 44,
"Location Full Name": "New Jersey",
"Location Status": "OPEN",
"Region/State": "NY",
"Postal Code": 10010,
"Country": "USA",
"Location Email Address": "email@domain.com",
"Location Telephone Number": "(123) 233 4453",
"Node Type": "Retail Store",
"User Login Location?": "Yes"
}];
for (let i = 0; i < arr.length; i++) {
let obj = {};
for (let [key, value] of Object.entries(arr[i])) {
filterKey = key.replace(/s+/g, "_")       // remove spaces
.replace(/W+/gm, "");  // remove special chars
obj[filterKey] = value;
}
arr[i] = obj;
}
console.log(arr);

最新更新