How can I convert a json object ["gen001", "abC0002"} to {"GEN001","ABC0002"] using Underscore.j?
var func = function(greeting){ return greeting + ': ' + this.name };
func = _.bind(func, {name: 'moe'}, 'hi');
func();
How can I convert a json object ["gen001", "abC0002"} to {"GEN001","ABC0002"] using Underscore.j?
var func = function(greeting){ return greeting + ': ' + this.name };
func = _.bind(func, {name: 'moe'}, 'hi');
func();
First, of all {"gen001", "abC0002"} is not a object.
If you need an object with keys gen001 and abC0002, when you should also specify values:
var input = {"gen001":"some value", "abC0002":"some other value"};
In that case you can use _.each to loop through object properties and build another object with the keys you want:
var input = {"gen001":"some value", "abC0002":"some other value"},
output = {};
_.each(
input,
function(element, index, list) {
output[index.toUpperCase()] = element;
}
);
console.log(output);
If you have an array:
var input = ["gen001", "abC0002"];
you can use _.map:
var input = ["gen001", "abC0002"];
var output = _.map(
input,
function(element) {
return element.toUpperCase();
}
);
console.log(output);
_.map uses _.each, so your second snippet works in both cases.input object, not keys. That's why it cannot be used without additional modifications.