删除对象值中多余的 space

Remove the extra space in object value

我正在尝试删除对象值中多余的 space。

const object = {
    primaryInformation: {
       firstName: "John  Doe",
       lastName: "Doe     Space"
    }
}

有什么最简单的方法可以使用map删除对象值单词之间多余的space吗?

预期输出

firstName: "John Doe",
lastName: "Doe Space" 

根据你问题中的对象结构,我写了这个循环。

const object = {
  primaryInformation: {
    firstName: "John  Doe",
    lastName: "Doe     Space"
  }
};

for (const key in object) {
  const value = object[key];

  for (const key2 in value) {
    const string = value[key2];

    value[key2] = string.replace(/  +/g, " ");
  }
}

console.log(object);