3

I have an array of JavaScript 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?

4
  • 2
    You can't change the name of variables and expect it to work Commented Oct 11, 2016 at 7:16
  • 1
    Are the values quoted? Or are they any other variable? Commented Oct 11, 2016 at 7:16
  • 1
    If it's a string, you can use .toLowercase() Commented Oct 11, 2016 at 7:16
  • 1
    Your title is saying uppercase but your question is saying lowercase. You should choose one maybe.. Commented Oct 11, 2016 at 7:18

7 Answers 7

8

If I understand your question correctly, you want to change the value of each object's 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.

See this fiddle for reference.

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

5 Comments

this will loop through all the values, so lets say you have an Array with 20 values it will update the key2 20 times.
@RobinKnaapen That's the idea yes. For instance, in the example he has two values in the array, they both need to be updated as per his output example.
oops, it's so early for me.. i didn't notice it was a multi dimensional object... my fault
Good job explaining the difference between for forEach and assign
If you use Object.assign the first argument will be the reference that is returned. So if you pass item to it, the reference will not change. Only if you call Object.assign({}, item, { key2: item.key2.toLowerCase() }), a new object will be created.
2
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.

Comments

1

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

Comments

1

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

Comments

1

You could iterate the array and the keys, you want to change, and assign the lowercase 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);

A version with a 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);

1 Comment

that would make all strings lowercase, not just key2
1

Concept:

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

  1. Create a function to lowercase the item based on the given key:

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

  2. Iterate items and parse each item on those function

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

  3. So, you will get newObj with lower case for the item with the 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));

1 Comment

Perhaps explain the "'' +" part in your answer? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)
0

Assuming you had a 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!

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.