Home > Back-end >  How change a value in an array of objects with the value of the array index?
How change a value in an array of objects with the value of the array index?

Time:01-08

I use react-beautiful-dnd. My first data is stored in a array of objects "characters" :

const characters = 
    [
    {id: 1, position: 0, type: 1, idLink: 2}, 
    {id: 2, position: 1, type: 1, idLink: 3}, 
    {id: 3, position: 2, type: 2, idLink: 1}, 
    {id: 4, position: 3, type: 1, idLink: 1},
    ]

When I move an element of my list, I send the result in a function to update the index in the array "characters".

function handleOnDragEnd(result) {
    if (!result.destination) return;
    const items = Array.from(characters);
    const [reorderedItem] = items.splice(result.source.index, 1);
    items.splice(result.destination.index, 0, reorderedItem);

    updateCharacters(items);
  }

Works fine my first element with "id:1" is now in the last postion :

const characters = 
        [ 
        {id: 2, position: 1, type: 1, idLink: 3}, 
        {id: 3, position: 2, type: 2, idLink: 1}, 
        {id: 4, position: 3, type: 1, idLink: 1},
        {id: 1, position: 0, type: 1, idLink: 2},
        ]

I try now many times to insert a new value for all "position". I need to change the value of "position" from its object's index value.

 function handleOnDragEnd(result) {
    if (!result.destination) return;
    const items = Array.from(characters);
    const [reorderedItem] = items.splice(result.source.index, 1);
    items.splice(result.destination.index, 0, reorderedItem);
    for (let i = 0; i < items.length; i  ) {
      items.splice(i, 1, result.destination.index);
    }
    updateCharacters(items);
  }

It doesn't work well. I need to have this :

const characters = 
    [
     
    {id: 2, position: 0, type: 1, idLink: 3}, 
    {id: 3, position: 1, type: 2, idLink: 1}, 
    {id: 4, position: 2, type: 1, idLink: 1},
    {id: 1, position: 3, type: 1, idLink: 2},
    ]

Any ideas ?

CodePudding user response:

Here is one way to achieve your goal:

const data2 = [
{name: "toto", position: null},
{name: "tata", position: null},
{name: "titi", position: null},
{name: "tutu", position: null},
];

for(let [index, value] of data2.entries()) {
  value.position = index;
}

console.log(data2);

CodePudding user response:

You could use the forEach method:

const data = [
{name: "toto", position: null},
{name: "tata", position: null},
{name: "titi", position: null},
{name: "tutu", position: null},
];

data.forEach((elem,id) => {
elem.position = id;
})

console.log(data);

CodePudding user response:

The array map method can supply an index to the callback function e.g.:

const characters = [ {id: 2, position: 1, type: 1, idLink: 3}, {id: 3, position: 2, type: 2, idLink: 1}, {id: 4, position: 3, type: 1, idLink: 1}, {id: 1, position: 0, type: 1, idLink: 2}, ];

const repositioned = characters.map(
  (character, index) => ({ ...character, position: index })
);

console.log(repositioned);

  • Related