How can I add object in array as value length?

I’d like to know how i can add object in array length.

I asked this question before, and I got the hint at the moment but i can’t still figure it out how to do it , so here is my piece code :

const [data, setdata] = useState([
    { x: 1, y: 2 },
    { x: 1, y: 2 },
    { x: 1, y: 2 },
  ]);
  
  
  const newData = [3, 4, 5, 6, 7, 8, 9];

const updateX = () => {
  setdata((data) => data.map((d, i) => ({ ...d, x: newData[i] })));
};


/////result ///


    { x: 3, y: 2 },
    { x: 4, y: 2 },
    { x: 5, y: 2 },




/// what i want ///


    { x: 3, y: 2 },
    { x: 4, y: 2 },
    { x: 5, y: 2 },
    { x: 6, y: 2 },
    { x: 7, y: 2 },
    { x: 8, y: 2 },
    { x: 9, y: 2 },

Answer

You can update like this:

const updateX = () => {
  setdata(newData.map((item, i) => ({ x: item, y: data[i] ?? 2 })));
};