2

Question: How to iterate through an array of objects and trim-specific property of an object?

Need to update the array of objects, to update every property in objects inside an array.

I need to delete white spaces (trim) accountId and handlerId properties in every object.

Here is a code example of what I tried:

// 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()
})

Any advice or help is welcome.

Kishan Patel
  • 778
  • 11
  • 26
Mark James
  • 338
  • 4
  • 15
  • 43
  • 1
    Possible duplicate of [Iterate over array of objects and change one property in each object](https://stackoverflow.com/questions/42306471/iterate-over-array-of-objects-and-change-one-property-in-each-object) – Dexygen Apr 01 '19 at 13:24

6 Answers6

4

You can use forEach and Object.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)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
3

You want to use 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);
CodeF0x
  • 2,624
  • 6
  • 17
  • 28
David
  • 3,552
  • 1
  • 13
  • 24
1

String#trim() returns the changed value. Since the array members are objects, you can directly modify the key, and assign the trimmed value. Moreover, i is the item, not the index, so you can directly access the properties on i, instead of data[i]. See documentation of Array#forEach.

Note: If you don't want to change the original items, you need to deep copy the array first, and then apply the forEach loop over copied object.

// 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);
31piy
  • 23,323
  • 6
  • 47
  • 67
1

You could take an array of the wanted keys for trimming and iterate the objects and the keys and assign the trimmed values.

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" }],
    keys = ['accountId', 'handlerId'];

data.forEach(o => keys.forEach(k => o[k] = o[k].trim()));

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You could use forEach method by passing a callback function as argument.

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);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

You could try something like this:

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))

and use it like this:

const newData = trimmer(data, ['accountId', 'handlerId'])
Viktor Pelle
  • 61
  • 1
  • 5