答案 0 :(得分:1)
如果要修剪对象的属性,可以直接在属性上使用trim()
函数。
var data = {
accountId: "1234567 ",
handlerId: "-LLUVULylrqhqq5QkMTU ",
}
data.accountId = data.accountId.trim();
data.handlerId = data.handlerId.trim();
console.log(data)
如果要修剪数组中所有对象的属性,可以使用forEach()
遍历数组,然后在属性上使用trim()
。
var dataArr = [
{
accountId: "1234567 ",
handlerId: "-LLUVULylrqhqq5QkMTU ",
},
{
accountId: "1234567 ",
handlerId: "-LLUVULylrqhqq5QkMTU ",
}
];
dataArr.forEach(d => {
d.accountId = d.accountId.trim();
d.handlerId = d.handlerId.trim();
});
console.log(dataArr);
答案 1 :(得分:1)
您可以使用Array.prototype.reduce()和Object.entries()不变地做到这一点。
Object.entries
遍历内部对象。reduce
遍历键/值对的列表:
accountId
和handlerId
并将它们分配给正确的属性。
const data = {
'COMP_011234567 NYWC-LLUVULylrqhqq5QkMTU': {
accountId: "1234567 ",
accountKey: "COMP_011234567 ",
accountName: "Test123 ",
adjusterClaimType: "WC",
adjusterKey: "COMP_01WC-LLUVULylrqhqq5QkMTU",
assignmentRulesKey: "COMP_011234567 NYWC",
companyAdjusterId: 1111111,
companyId: "COMP_01",
exception: false,
handlerId: "-LLUVULylrqhqq5QkMTU",
key: "COMP_011234567 NYWC-LLUVULylrqhqq5QkMTU",
lineOfBusiness: "WC",
name: "WC Med Only Direct Without skills - All states",
selectedState: "NY",
tpa: "no",
tpaCompany: ""
}
};
const result = Object.entries(data).reduce((acc, [key, x]) => {
acc[key] = { ...x, accountId: x.accountId.trim(), handlerId: x.handlerId.trim() };
return acc;
}, {});
console.log(result);