title: How to convert object's field name to value in Javascript
body: <p>I'm trying to convert an object like <code>{fieldName: "value"}</code> into another object like <code>{field: "fieldName", value: "value"}</code> in the simplest way possible, without knowing what the fieldName is in advance.</p>
  
  <p>I've looked at the answer to <a href="https://stackoverflow.com/questions/4841254/how-to-convert-string-as-objects-field-name-in-javascript/4841286#4841286">How to convert string as object&#39;s field name in javascript</a> but although this works it relies on the fact that the fieldName is already known. The following works:</p>
  
  <pre><code>const key = Object.keys(searchObject)[0];
    return { field: key, value: searchObject[key] }
  </code></pre>
  
  <p>But it looks unwieldy, clunky and error-prone. What I would like is the equivalent of </p>
  
  <pre><code>const obj = { field: [searchObject.key], ... }
  </code></pre>
  
  <p>But this only works where <code>[searchObject.key]</code> is the <em>value</em> of <code>searchObject.key</code>.</p>