Add / Overwrite an object in an array
From a short review;

The first snippet does not work, try it with inputTitle = 'BBB'

You are in essence doing 2 things

Find a possible match
Update the match or insert the data

Ideally, this is done in 1 'loop'

If you don't care too much about performance, I would go for a functional approach


This is my counter-proposal (which modifies the data)


const data = [
  {
    title: 'AAA',
    people: [ 'John', 'Megan',]
  },{
    title: 'BBB',
    people: [ 'Emily', 'Tom']
  }
]

function mergePeople(data, title, people){
  const target = data.filter(record=&gt;record.title===title);
  if(target.length){
    target.forEach(record =&gt; record.people = people);
  }else{
    data.push({title, people});
  }
  return data;
}

function quickClone(o){
  return JSON.parse(JSON.stringify(o));
}

console.log(mergePeople(quickClone(data), "AAA", ["Tom", "Jerry"]));
console.log(mergePeople(quickClone(data), "BBB", ["Ben", "Jerry"]));