Sara Ree
Sara Ree

Reputation: 3543

Capitalize string elements inside arrays that are inside an object

I have this object:

let result = {
'i was sent': ["I was sent", "you were Awesome!"],
'to earth': ["to Earth" , "To moon"],
'to protect you': ["to protect you"]
}

What if we want to capitalize each first letter of all elements of the arrays and return the result like :

 let result = {
    'i was sent you to': ["I was sent", "You were Awesome!"],
    'to earth': ["To Earth" , "To moon"],
    'to protect you': ["To protect you"],
 }

I have tried this with no success (Note that capitalize function works fine for strings):

Object.keys(result).forEach(function(key) {

     result[key].forEach(function(e) {
     capitalize(result[key][e]);
     });
});

console.log(result)

function capitalize(s) {
    let first_char = /\S/;
    return s.replace(first_char, function(m) {
    return m.toUpperCase();
 });

}

Upvotes: 0

Views: 95

Answers (3)

ggorlen
ggorlen

Reputation: 56965

If you'd like to do this in-place, a reassignment needs to happen to replace the original values. You can loop over the entries in the object with for ... in and assign the result of map to the object key.

const data = {
  'i was sent': ["I was sent", "you were Awesome!"],
  'to earth': ["to Earth" , "To moon"],
  'to protect you': ["to protect you"]
};

for (const k in data) {
  data[k] = data[k].map(e => e.replace(/\S/, m => m.toUpperCase()));
}

console.log(data);

If you don't want to mutate the original, you can use Object.fromEntries:

const data = {
  'i was sent': ["I was sent", "you were Awesome!"],
  'to earth': ["to Earth" , "To moon"],
  'to protect you': ["to protect you"]
};

const capitalize = s => s.replace(/\S/, m => m.toUpperCase());
const capitalizedData = Object.fromEntries(Object.entries(data).map(([k, v]) =>
  [k, v.map(capitalize)]
));

console.log(capitalizedData);

Upvotes: 1

Emiel Zuurbier
Emiel Zuurbier

Reputation: 20944

It is not working because you are not reassigning the values to the properties in the result object. Use the map method of the arrays to create a new array with capitalized strings and reassign it to the keys in the object.

function capitalize(s) {
  let first_char = /\S/;
  return s.replace(first_char, function(m) {
    return m.toUpperCase();
  });
}

let result = {
  'i was sent': ["I was sent", "you were Awesome!"],
  'to earth': ["to Earth" , "To moon"],
  'to protect you': ["to protect you"]
};

for (const [ key, value ] of Object.entries(result)) {
  result[key] = value.map(capitalize);
}

console.log(result);

Upvotes: 1

Barmar
Barmar

Reputation: 781088

capitalize() doesn't modify the string in place, you need to use the returned value. You can use .map() to create an array of results and assign this back to the object element.

let result = {
  'i was sent': ["I was sent", "you were Awesome!"],
  'to earth': ["to Earth", "To moon"],
  'to protect you': ["to protect you"]
}

Object.keys(result).forEach(function(key) {
  result[key] = result[key].map(capitalize);
});

console.log(result)

function capitalize(s) {
  let first_char = /\S/;
  return s.replace(first_char, function(m) {
    return m.toUpperCase();
  });
}

Upvotes: 5

Related Questions