Jon
Jon

Reputation: 445

Convert All String Values of an Object to Lowercase Javascript

I would like to take an array of objects that contains string and numeric values and convert all string values to lowercase. I don't know ahead of time what specific values my array objects will have, so I just want to first check if an object within the array contains a string, and then make that string lowercase. When I run the following, I get an error.

let data = [{
    userId: 1234567890,
    errorId: 957,
    userCategory: "Category",
    userAge: 18,
    userType: "Standard"
  },
  {
    userId: 1234567890,
    errorId: 583,
    userCategory: "Second",
    userAge: 28,
    userType: "Superuser"
  },
  {
    userId: 1234567890,
    errorId: 823,
    userCategory: "Third",
    userAge: 38,
    userType: "Admin"
  }
]

let newData = data.map(x => x.toLowerCase());

console.log(newData) //data is not a function

Upvotes: 1

Views: 2893

Answers (3)

Scrapper142
Scrapper142

Reputation: 586

Array.map is not needed here (some other answers suggest this) since you are not changing what the objects themselves are, only their properties.

Iterate over the array, changing the objects.

let data = [{
  userId: 1234567890,
  errorId: 957,
  userCategory: "Category",
  userAge: 18,
  userType: "Standard"
}, {
  userId: 1234567890,
  errorId: 583,
  userCategory: "Second",
  userAge: 28,
  userType: "Superuser"
}, {
  userId: 1234567890,
  errorId: 823,
  userCategory: "Third",
  userAge: 38,
  userType: "Admin"
}];

for (let obj of data) { // for each loop, iterates over values
  for (let key in obj) { // iterates over the keys
    if (typeof obj[key] === 'string') {
      obj[key] = obj[key].toLowerCase();
    }
  }
}

console.log(data);

Upvotes: 1

Tom O.
Tom O.

Reputation: 5941

You could simply iterate over the object keys and check the typeof it's value - if it's a string then call toLowerCase on it, otherwise just return the original data. For example:

let data = [{
    userId: 1234567890,
    errorId: 957,
    userCategory: "Category",
    userAge: 18,
    userType: "Standard"
  },
  {
    userId: 1234567890,
    errorId: 583,
    userCategory: "Second",
    userAge: 28,
    userType: "Superuser"
  },
  {
    userId: 1234567890,
    errorId: 823,
    userCategory: "Third",
    userAge: 38,
    userType: "Admin"
  }
]

const newData = data.map(el => {
  const newEl = {};

  for (const key in el) {
    newEl[key] = typeof el[key] === typeof '' ? el[key].toLowerCase() : el[key]
  }

  return newEl;
});

console.log(newData);

Upvotes: 0

Barmar
Barmar

Reputation: 782106

You need to use .map() to iterate over the array, you can't just use the array itself as a function.

You can then use Object.entries(x).map() to iterate of the keys and values of the object, and lowercase the string values. A new object can then be created using Object.fromEntries().

let data = [{
    userId: 1234567890,
    errorId: 957,
    userCategory: "Category",
    userAge: 18,
    userType: "Standard"
  },
  {
    userId: 1234567890,
    errorId: 583,
    userCategory: "Second",
    userAge: 28,
    userType: "Superuser"
  },
  {
    userId: 1234567890,
    errorId: 823,
    userCategory: "Third",
    userAge: 38,
    userType: "Admin"
  }
]

let newData = data.map(x => Object.fromEntries(Object.entries(x).map(
  ([key, value]) => [key, typeof value == 'string' ? value.toLowerCase() : value])));

console.log(newData)

Upvotes: 2

Related Questions