Merge arrays in objects in array based on property
A short review;

This code looks incredibly complicated for what you want to achieve, sometimes a few for loops beat the functional approach
array is a global variable (not var or set or const), that is a bad thing
If a function is not inline, I would encourage you to use the function keyword
I am not sure mergeLines is a great name, perhaps mergeIds ?



array = [{
    id: 1,
    items: [1, 2, 3]
  },
  {
    id: 2,
    items: [1, 2]
  },
  {
    id: 1,
    items: [4]
  },
  {
    id: 3,
    items: [1]
  },
];

function mergeIds(list){
  const out = [];
  for(entry of list){
    const existingEntry = out.find(o =&gt; o.id === entry.id);
    if(existingEntry){
      existingEntry.items = existingEntry.items.concat(entry.items);
    } else {
      out.push(entry);
    }
  }
  
  return out;
}

console.log(mergeIds(array));