swap some values in an array of objects while maintain sorted others

Francesco Bellavita

let's say we have an array like this:

[
  {l:A,n:1},
  {l:B,n:2},
  {l:C,n:3},
  {l:D,n:4},
  {l:E,n:5}
]

I want to change order of 2 values: C become 5 and D become 2.

If i use a cycle like this to swap values...

const arr = [
  {l:A,n:1},
  {l:B,n:2},
  {l:C,n:3},
  {l:D,n:4},
  {l:E,n:5}
]

const changes = [
  {l:C,n:5},
  {l:D,n:2}
]

changes.forEach(change => {
  const changedI = arr.findIndex( x => x.l == change.l)
  const swappedI = arr.findIndex( x => x.n == change.n)
  const changedN = arr[changedI].n
  arr[changedI].n = change.n
  arr[swappedI].n = changedN
})
the result is:
[
  {l:A,n:1},
  {l:B,n:4},
  {l:C,n:5},
  {l:D,n:2},
  {l:E,n:3}
]

But i need that the result array is ordered (only) for non changed elements, like this:

[
  {l:A,n:1},
  {l:B,n:3},
  {l:C,n:5},
  {l:D,n:2},
  {l:E,n:4}
]

therefore only the changed elements remain fixed, while the elements not expressly changed are rearranged from the smallest to the largest.

I was thinking of doing it with two loops where on the first i swap the slots, and on the second i rearrange only the unchanged elements, but I think there is a faster way, perhaps with a recursion?

Nina Scholz

You could take a single loop approach and store open object who need to get a new number and another for missing numbers.

With

change = [['C', 5], ['D', 2]]

You get

data = [                        v
    { l: 'A', n: 1 }, //        1
    { l: 'B', n: 2 }, //        3
    { l: 'C', n: 3 }, // <- 5   5
    { l: 'D', n: 4 }, // <- 2   2
    { l: 'E', n: 5 }  //        4
]

With

change = [['C', 1]]

You get

data = [                        v
    { l: 'A', n: 1 }, //        2
    { l: 'B', n: 2 }, //        3
    { l: 'C', n: 3 }, // <- 1   1
    { l: 'D', n: 4 }, //        4
    { l: 'E', n: 5 }  //        5
]

const
    data = [
        { l: 'A', n: 1 }, //        2
        { l: 'B', n: 2 }, //        3
        { l: 'C', n: 3 }, // <- 1   1
        { l: 'D', n: 4 }, //        4
        { l: 'E', n: 5 }  //        5
    ],
    //change = [['C', 5], ['D', 2]],
    change = [['C', 1]],
    open = [],
    missing= [];


data.forEach(o => {
    if (change.some(([, v]) => v === o.n)) {
        if (missing.length) o.n = missing.shift();
        else open.push(o);
        return;
    }
    const pair = change.find(([k]) => k === o.l);
    if (pair) {
        if (open.length) open.shift().n = o.n;
        else missing.push(o.n);
        o.n = pair[1];
        return;
    }
    open.push(o);
    open.shift().n = o.n;
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Efficient way to swap property values in array of objects

Why are some array values being overwritten and others are not? PHP

How to safely swap values of two objects in array in one request?

How to load some values to an instance of an array of objects?

Some of my dataframe values include dictionaries, while others contain values. How do I remove the dictionaries?

Sanitize some translate values but not others

Swift swap array objects

Filter array of objects by values when not all objects have some keys

How to group pandas DataFrame if some values are range of integers, while others are pure integer?

Swap array values in Swift

Swap the values in a MongoDB array

Why do some processes use swap and others don't?

How to collide with some objects and ignore others? Unity

Searching a swap-sorted array of distinct integers

.htaccess: Some Rewrites working while others not

How to plot only some cells with certain values of an array and others not with matplotlib.pyplot?

Subset does not work with some numeric values but with others

why for some values there is an infinity result but no for others?

Elasticsearch must some values and must not all others

matching on some values but not others in nested dict

DLookup returning True for some values, and False for others

Change some object keys on an array and maintain order

How to keep a fixed size of unique values in random positions in an array while replacing others with a mask?

How to use .some() for all values in an array with different objects

Array of objects with some objects containing another array, need to filter it by values within the object containing an array

Hold some values ​in position while sorting the array in PHP

Maintain Array of Objects when using product method

How to "swap" two values in an array?

How to swap values of an array? Javascript