I have a function that processes arrays, and puts them into a specific format at the end. I do this by cycling through the processed-but-not-formatted output, formatting it into a JSON and pushing it to an array. However, when I push it to the array, every other item already in the array changes to be what I've just pushed. Why, and how can I fix this? It's probably something silly that I missed, but I can't figure out the cause of the issue.
function removeLastXItems(newArr, i, format, key){
  newArr.splice(newArr.length-i-1, i);
  var toInsert, finalArr = [];
  for (b=0;b<newArr.length;b++){
    toInsert = format;
    toInsert[key] = newArr[b];
    finalArr.push(toInsert);
    console.log(finalArr)
  }
  return finalArr;
}
When I run removeLastXItems(["foo", "bar", "fob", "bof", "far", "boo"], 2, {itGoes:"here", andNot:"here"}, "itGoes"), I'd expect the output to be:
[
  { itGoes: "foo",
    andNot: "here"
  },
  { itGoes: "bar",
    andNot: "here"
  },
  { itGoes: "fob",
    andNot: "here"
  },
  { itGoes: "bof",
    andNot: "here"
  }
]
However, the actual output is:
[ 
  { itGoes: 'boo', 
    andNot: 'here' },
  { itGoes: 'boo', 
    andNot: 'here' },
  { itGoes: 'boo', 
    andNot: 'here' },
  { itGoes: 'boo', 
    andNot: 'here' } 
]
Clearly, the result is changing, as if I look in the console I see:
[ 
  { 
    itGoes: 'foo', 
    andNot: 'here' 
  } 
]
[ 
  { 
    itGoes: 'bar', 
    andNot: 'here' 
  },
  { 
    itGoes: 'bar', 
    andNot: 'here' 
  } 
]
[
  { 
    itGoes: 'fob', 
    andNot: 'here' 
  },
  { 
    itGoes: 'fob', 
    andNot: 'here' 
  },
  { 
    itGoes: 'fob', 
    andNot: 'here' 
  } 
]
[ 
  {
    itGoes: 'boo', 
    andNot: 'here' 
  },
  { 
    itGoes: 'boo', 
    andNot: 'here' 
  },
  { 
    itGoes: 'boo', 
    andNot: 'here' 
  },
  { 
    itGoes: 'boo', 
    andNot: 'here' 
  } 
]
as obvious, the other items in the array are changing each time I push!
I've looked at this similar post, but I've not committed any of the mistakes outlined in the answers there. Therefore, if someone can find my mistake, I'd greatly appreciate it!
