title: The fastest way to find an element in an array with a given property name and replace it
body: <p>I have a performance issue with NgRx, I have an array with thousands of objects that looks like this (I can't change that structure even I don't like it):</p>
  <p><code>state.alarms</code> structure:</p>
  <pre><code>[
   { global: {...} },
   { 282: {...} }, 
   { 290: {...} },
   { 401: {...} }
   etc...
  ]
  </code></pre>
  <p><a href="https://i.stack.imgur.com/3InWH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3InWH.png" alt="enter image description here" /></a></p>
  <p><code>addNewAlarm(state, alarm)</code> here alarm object is for example:</p>
  <pre><code>{ 282: {...} }
  </code></pre>
  <p>As you can see the object looks something like this <code>{ someNumber: nestedObjectForThatNumber }</code></p>
  <p>I'm listening for changes and if some appear I have to replace object where &quot;the key&quot; is given number.</p>
  <p>In the case from the screenshot for example I get <code>{ 282: {x: 1, y: 2, z: 3} }</code> so I have to replace the item of array with index 1.</p>
  <p>In my reducer I've created something like this but it doesn't work as I expected:</p>
  <pre><code>export function addNewAlarm(state: State, alarm: AlarmsObject): State | undefined {
    const alarms: AlarmsObject[] = [...state.alarms];
    if (state) {
      const existingRecord = state.alarms.find(alarm1 =&gt; alarm1.hasOwnProperty(Object.keys(alarm)[0]));
      if (existingRecord) {
        const index = state.alarms.indexOf(existingRecord);
        alarms[index] = alarm;
      }
    }
  
    return { ...state, alarms };
  }
  </code></pre>
  <p>Maybe someone can give me a hint how to do it in a right way?</p>