简体   繁体   中英

How to lowercase Javascript object values?

I have an array of JS objects:

var obj = [{key1: Value1, key2: VALUE2},{key1: Value1, key2: VALUE2}]; 

I want the value of the key2 to be in lowercase, like this:

var obj = [{key1: Value1, key2: value2},{key1: Value1, key2: value2}]; 

How can I change the value from lowercase to uppercase?

If I understand your question correctly, you want to change the value of each objects key2 property, to be a lowercase string (I'm assuming they're strings.)

You can do this with a simple map.

obj = obj.map(function(a) { 
    a.key2 = a.key2.toLowerCase();
    return a;
});

There are some differences in all the answers given here, surrounding object references. In my answer above, the array itself will be a new reference, but the objects inside will remain as the same reference. To clarify this by way of example

var mapped = obj.map(function(a) { 
    a.key2 = a.key2.toLowerCase();
    return a;
});

console.log(obj === mapped) // false
console.log(obj[0] === mapped[0]) // true

If you're using a for loop or a forEach array function, then the array reference will remain the same.

If you are using Object.assign as in @Piotrs answer, the object references themselves will change, example:

 
 
 
  
  var mapped = obj.map(function (item) { return Object.assign(item, { key2: item.key2.toLowerCase() }); }); console.log(obj[0] === mapped[0]); //false
 
 

Thanks to @user3297291

For pointing out that Object.assign does not actually change the object reference or create a new one, it instead performs the assignment on the first argument of the assign method, and returns a reference to that object.

var item = { key1: 'value1', key2: 'VALUE2' };

var newItem = Object.assign(item, { key2: item.key2.toLowerCase() });
console.log(newItem === item); // true

var newRef = Object.assign({}, item, { key2: item.key2.toLowerCase() });
console.log(newRef === item); // false

See this fiddle for reference

You could iterate the array and the keys, you want to change, and assign the lower case value if you have a string.

 var array = [{ key1: 'Value1', key2: 'value2' }, { key1: 'Value1', key2: 'value2' }]; array.forEach(function (a) { ['key2'].forEach(function (k) { if (typeof a[k] === 'string') { a[k] = a[k].toLowerCase(); } }); }); console.log(array);

Version with fixed key.

 var array = [{ key1: 'Value1', key2: 'value2' }, { key1: 'Value1', key2: 'value2' }]; array.forEach(function (a) { if (typeof a.key2 === 'string') { a.key2 = a.key2.toLowerCase(); } }); console.log(array);

You can use built-in array functions like forEach or map

// in-place
obj.forEach(function (item) {
    item.key2 = item.key2.toLowerCase()
})

or

var mapped = obj.map(function (item) {
    return Object.assign(item, { key2: item.key2.toLowerCase() });
})

You just have to loop through the array and lowercase the value of key2 for every object.

var objects = [{key1: "Value1", key2: "VALUE2"},{key1: "Value1", key2: "VALUE2"}]; 

for(var i = 0;i < objects.length;++i) {
    objects[i].key2 = objects[i].key2.toLowerCase();
}

Concept: Create a function to lowercase an item based on given key. Then, iterate items and parse each item on those function.

  1. Create a function to lower case item based on given key

function lowerObj(obj, key) { obj[''+key] = obj[''+key].toLowerCase(); return obj; }

  1. Iterate items and parse each item on those function

var newObj = obj.map((item) => {return lowerObj(obj, 'your_key')});

  1. So, you will get newObj with lower case for item with given key.

Example

 function lowerObj(obj, key) { obj[''+key] = obj[''+key].toLowerCase(); return obj; } var obj = [{key1: 'Value01', key2: 'Value02'}, {key1: 'Value11', key2: 'Value12'}]; var newObj = obj.map((item) => {return lowerObj(item, 'key2')}); console.log(JSON.stringify(newObj, null, 3));

    caseInsensitivity=function(obj){


        Object.keys(obj).forEach(k=>{

            if(typeof obj[k]=='string'){

                    obj[k]=obj[k].toLowerCase();



            }
            else if(typeof obj[k]=='object'){

                caseInsensitivity(obj[k]);
            }else{

            }
        });return obj;

}

to convert all values even if the values are nested objects or arrays

Assuming you had json Object like.

"params": {
  "name":"LOWERNAME"
  "phone": "1234567899"
       }

Call your Object like:

    let user = {
    name: params.name.toLowerCase(),
    phone: params.phone
               }

You could modify it to suit your situation !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM