对象数组内的特定对象的trim属性

时间:2019-04-01 13:22:43

标签: javascript

问题:如何遍历对象数组和对象的修剪特定属性?

需要更新对象数组,以更新数组中对象的每个属性。

我需要删除每个对象中的空格(修剪)accountId和handlerId属性。

这是我尝试过的代码示例:

// This is an array of objects
var data = [
  { 
    Company: "Church Mutual",
    accountId: "1234567  ",  <======= need to trim()
    accountName: "Test123",
    handlerId: "1111111  ",   <======= need to trim()
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  },
   { 
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  }
];

var newData = data.forEach(i => {
 data[i].accountId.trim()
})

欢迎任何建议或帮助。

6 个答案:

答案 0 :(得分:1)

您要使用map

var data = [
  { 
    Company: "Church Mutual",
    accountId: "1234567  ",  
    accountName: "Test123",
    handlerId: "1111111  ",   
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  },
   { 
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  }
];

var newData = data.map(o => {
 o.accountId = o.accountId.trim();
 o.handlerId = o.handlerId.trim();
 return o;
});

console.log(newData);

答案 1 :(得分:1)

String#trim()返回更改后的值。由于数组成员是对象,因此可以直接修改键并分配修剪后的值。此外,i是项目,而不是索引,因此您可以直接访问i上的属性,而不是data[i]。请参阅Array#forEach的文档。

注意:如果您不想更改原始项目,则需要先deep copy数组,然后在复制的对象上应用forEach循环。

// This is array of objects
var data = [{
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  },
  {
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  }
];

data.forEach(i => {
  i.accountId = i.accountId.trim();
  i.handlerId = i.handlerId.trim();
})

console.log(data);

答案 2 :(得分:1)

您可以使用forEachObject.entries

var data = [
  { 
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  },
   { 
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  }
];

data.forEach(e => {
  Object.entries(e).forEach(([key,value])=>{
    e[key] = value.trim()
  })
})

console.log(data)

答案 3 :(得分:1)

您可以采用所需的键数组进行修剪,并迭代对象和键并分配修剪后的值。

require
jest.resetModules

答案 4 :(得分:1)

您可以通过传递 callback 函数作为参数来使用forEach方法。

var data = [ { Company: "Church Mutual", accountId: "1234567 ", accountName: "Test123", handlerId: "1111111 ", lineOfBusiness: "WC", selectedState: "NY", tpa: "No", }, { Company: "Church Mutual", accountId: "1234567 ", accountName: "Test123", handlerId: "1111111 ", lineOfBusiness: "WC", selectedState: "NY", tpa: "No", } ];

data.forEach(item => { 
    Object.keys(item).forEach(key => { 
        item[key] = item[key].trim()
    });
});

console.log(data);

答案 5 :(得分:1)

您可以尝试这样的事情:

var data = [{
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  },
  {
    Company: "Church Mutual",
    accountId: "1234567  ",
    accountName: "Test123",
    handlerId: "1111111  ",
    lineOfBusiness: "WC",
    selectedState: "NY",
    tpa: "No",
  }
];
const trimmer = (data, fields) => data.map(o => fields.reduce((acc, field) => Object.assign(acc, {[field]: acc[field].trim()}),o))

并像这样使用它:

const newData = trimmer(data, ['accountId', 'handlerId'])