How i can update object in array

kim tech

I'd like to know how i can update object in array.

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

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


/////result ///


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




/// what i want ///


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

Xabi

I don't quite understand why you are initializing the data object with those values.
The result you want, you can't get it by iterating the data object that you have initialized with 3 values.

Assuming that the arrays have these values and an equal length, you can try this:

const [data, setdata] = useState([
    { x: 1, y: 2 },
    { x: 1, y: 2 },
    { x: 1, y: 2 }
  ]);

  const x = [3, 4, 5, 6, 7, 8, 9];
  const y = [1, 2, 3, 4, 5, 6, 7];

  const newData = x.reduce((acc, xVal, index) => {
    const obj = { x: xVal, y: y[index] };
    acc.push(obj);
    return acc;
  }, []);

  setdata(newData)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How i can update or put a new object into array of object? For Beginners

How can I update a json object inside of an array?

How can I update an object inside of an array in firestore?

How can I update a state inside an array of object with Redux?

How can i update an object (map) in an array with swift in firestore database?

How can I update multiple array object field in mongoose?

how can i dynamically update my nested object within another object, within an array?

How can I map an Array or an Object to an Object?

How can I turn an array object into an object?

how can i update an object id in vuejs

How can I loop through an Object of Objects and an Array of Objects and update nested object values based on the count of matching array elements?

How can I update value of array in database

How can i update a Angular Form Array

How can i update the name in an array of objects?

How can I filter an array to update tableview?

how can i update this array in MongoDB?

how can i update field in array?

How can i update array value in mongodb?

How can i update array's element?

How can i update a useState from an array?

How can I update a document's nested array of object item by 1?

how can i change array of object to object of array in javascript?

how can I filter array in object?

How can I pass the array object?

how can i convert object into an array in javascript?

How can i convert array to SimpleXMLElement Object

How can I turn an object into an array (Symfony)?

How can I access an array/object?

How can I check if an object is an array?