1

I have array of map objects, want to update two properties of each object.

  1. accountId
  2. handlerId

Updated:

Here is how array of map objects looks:

Image of console data

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: ""
    }
}

I need to trim() accountId and handlerId.


// Already tried this apporach: 

  data.forEach(i => {
    i.accountId = i.accountId.trim();
    i.handlerId = i.handlerId.trim();
  })
5
  • 2
    The original object is quite unclear. I mean, the structure is unclear. Can you please share a better log of it? Commented Apr 1, 2019 at 15:30
  • Any errors in console when you try the trim shown? Should work fine if those properties are direct children of i. Please provide a runnable minimal reproducible example Commented Apr 1, 2019 at 15:31
  • Your object doesnt actually look like an array in that screenshot. It looks like a plain object Commented Apr 1, 2019 at 15:34
  • right, my bad in description of question. Commented Apr 1, 2019 at 15:37
  • You're trying to use Array functions on an object....not going to work Commented Apr 1, 2019 at 15:37

2 Answers 2

1

You can do it immutably with Array.prototype.reduce() and Object.entries().

  1. Iterate over your inner objects with Object.entries.
  2. Iterate over this list of key/values pairs with reduce:
    1. For each entry, map the object to a shallow copy with the spread operator.
    2. Then trim accountId and handlerId and assign them to the right properties.
    3. Finally, assign the new object to the right key in the accumulator.

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

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to trim property from an object, you can directly use trim() function on the property.

var data = {
  accountId: "1234567      ",
  handlerId: "-LLUVULylrqhqq5QkMTU    ",
}

data.accountId = data.accountId.trim();
data.handlerId = data.handlerId.trim();
console.log(data)

If you want to trim properties from the all the objects in an array, you can iterate over the array using forEach() and then use trim() on the property.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.