Changing the case of JavaScript object keys

convert json keys to lowercase javascript
javascript convert object values to lowercase
javascript rename object key
javascript convert object keys to camelcase
javascript object has keys
how to change key = value in object javascript
javascript get object properties
javascript array keys

I have following object.

var obj = [{
  Address1: "dd",
  Address2: "qww",
  BankAccNo: "44",
  BankBranchCode: "44",
  BloodGrp: "A+"
},
{
  Address1: "dd",
  Address2: "qww",
  BankAccNo: "44",
  BankBranchCode: "44",
  BloodGrp: "A+"
}];

How can I make all of the keys uppercase?

I want to be able to access values like this : - obj[0].ADDRESS1

obj = obj.map( function( item ){
    for(var key in item){
        var upper = key.toUpperCase();
        // check if it already wasn't uppercase
        if( upper !== key ){ 
            item[ upper ] = item[key];
            delete item[key];
        }
    }
    return item;
});

http://jsfiddle.net/07xortqy/

Changing the case of JavaScript object keys, obj = obj.map( function( item ){ for(var key in item){ var upper = key.toUpperCase​(); // check if it already wasn't uppercase if( upper !== key ){  Change the case of the keys of a javascript object - paulovieira/change-case-keys

  1. Loop over all the properties in the object (with for in)
  2. Use .toUpperCase() to get the uppercase version of the property name
  3. Copy the value from the original property to the uppercase version
  4. delete the original property

Object keys to lowercase, If you can go ES6, you can get a more concise version: Object.keys(o).reduce((c, k) => (c[k.toLowerCase()] = o[k], c), {});. Now, if one or more  In this approach we will rename the given object key by utilizing defineProperty() to manipulate the property of the object. defineProperty(): This static method is used to define a new property of an object or modify an existing one, and returns the object.

For anyone looking for a solution working with objects, arrays, and nested objects or arrays:

// rename function depending on your needs
const capitalizeKeys = (obj) => {
  const isObject = o => Object.prototype.toString.apply(o) === '[object Object]'
  const isArray = o => Object.prototype.toString.apply(o) === '[object Array]'
  
  let transformedObj = isArray(obj) ? [] : {}
  
  for (let key in obj) {
    // replace the following with any transform function
    const transformedKey = key.replace(/^\w/, (c, _) => c.toUpperCase())

    if (isObject(obj[key]) || isArray(obj[key])) {
      transformedObj[transformedKey] = capitalizeKeys(obj[key])
    } else {
      transformedObj[transformedKey] = obj[key]
    }
  }
  return transformedObj
}

const t = {
  test1: 'hello',
  test2: {
    aa: 0,
    bb: '1',
    cc: [ 3, '4', 'world']
  },
  test3: [{
      aa: 5,
      bb: '6'
    }, {
      cc: [ 'hello', 'world', 7 ]
    }
  ]
}

console.log(JSON.stringify(capitalizeKeys(t)))

convert all keys a of javascript object to lowercase with lodash · GitHub, convert all keys a of javascript object to lowercase with lodash. gistfile1.txt. _.​transform(obj, function (result, val, key) {. result[key.toLowerCase()]  The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.

$.each(obj, function(i, parent) {
  $.each(parent, function(key, record) {
    parent[ key.toUpperCase() ] = record[key]; //rename key
    delete parent[key]; //delete old key
  });
});

30 Seconds of Code: How to rename multiple object keys in JavaScript, 30 Seconds of Code is a brilliant collection of JavaScript snippets, digestible in ≤ 30 I've previously written on renaming object keys, but we only changed one key at a time. In this case, keysMap[key] is “firstName” . Edit: See how to rename many object keys here.. If you’re okay with mutating data, renaming an object’s key is easy. obj = { name: 'Bobo' } obj.somethingElse = obj.name delete obj.name

let obj = [
{ Address1: "dd",Address2: 'qww',BankAccNo: 44,BankBranchCode: 44,BloodGrp: 'A+' },
{ Address1: "dd",Address2: 'qww',BankAccNo: 44,BankBranchCode: 44,BloodGrp: 'A+' }
];

const uppercaseKeys = (elem) => {
  let newObject = {}

  Object.keys(elem).reduce( (acc, key, allKeys) => {
    acc[key.toUpperCase()] = elem[key]
    delete elem[key]
    return acc
  }, elem)

  return newObject
}

obj.forEach( o => uppercaseKeys )
console.log(obj)

Object.keys( ) In JavaScript, Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects)  Syntax Object.keys(obj)Parameters obj The object of which the enumerable's own properties are to be returned. Return value. An array of strings that represent all the enumerable properties of the given object.

Rename object key in JavaScript, Asymptotic Analysis · Worst, Average and Best Cases · Asymptotic Notations · Little o and JavaScript doesn't provide an inbuilt function to rename an object key. Keys are known as 'property name' and are used to identify values. This static method is used to define a new property of an object or modify an existing one  Introduction. An object in JavaScript is a data type that is composed of a collection of names or keys and values, represented in name:value pairs.The name:value pairs can consist of properties that may contain any data type — including strings, numbers, and Booleans — as well as methods, which are functions contained within an object.

Object, Changes to the Object prototype object are seen by all objects through prototype Object.keys(): Returns an array containing the names of all of the given When a function is called, the arguments to the call are held in the  Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.

Working with objects, A property's value can be a function, in which case the property is known as a method. A JavaScript object has properties associated with it. Switching Details. If multiple cases matches a case value, the first case is selected.. If no matching cases are found, the program continues to the default label.. If no default label is found, the program continues to the statement(s) after the switch.

Comments
  • Why not just access them as they are?
  • @nnnnnn maybe because of consistency with some DB name fields, just guessing
  • Yes...I am matching this keys with values stored in DB, which are in Uppercase.!
  • This won't work if the key is already upper case. And I'm not sure how it will work on every browser because you change an object while looping on it with a for in
  • @SebastienC. thanks, added a safeguard for existing uppercase keys.
  • you complete pawel's answer nicely
  • This looks what I want.
  • Please add explanation to your answer. Just code lines do not constitute a good answer.
  • first iterate array then iterate keys, inside second loop rename key and delete old one.
  • Edit your question instead of adding a comment.
  • Using for..in to iterate over arrays is frowned upon. And now you have both upper- an lower-case keys, which may or may not be what OP wanted.