Efficient way to swap property values in array of objects

Florian

Assume we have the following array of Objects (the real has 71000 elements, but 4 objects are enough to give you an idea):

[
    {
        source: "France"
        target: "Morocco"
        timeN: "2008"
        valueN: "252.35"
    },
    {
        source: "France"
        target: "Morocco"
        timeN: "2009"
        valueN: "424.12"
    },
    {
        source: "France"
        target: "Morocco"
        timeN: "2010"
        valueN: "152.24"
    },
    {
        source: "France"
        target: "Morocco"
        timeN: "2011"
        valueN: "-342.19"
    }
]

How can I efficiently swap source and target values on the last object, if valueN is negative? I would like to just multiply valueN with -1 or call Math.abs() and then change also source to "Morocco" and target to "France".

Update

Although all answers are great and I have taken the clean one by @Nina Scholz. However, the one from @Emil S. Jørgensen is the most performant one, once tracking the execution time. I don't know why but it seems to be most performant if you do not convert it back to a string, the valueN.

Nina Scholz

Iterate. Check. Swap. Update.

var array = [{ source: "France", target: "Morocco", timeN: "2008", valueN: "252.35" }, { source: "France", target: "Morocco", timeN: "2009", valueN: "424.12" }, { source: "France", target: "Morocco", timeN: "2010", valueN: "152.24" }, { source: "France", target: "Morocco", timeN: "2011", valueN: "-342.19" }];

array.forEach(o => {
    if (o.valueN < 0) {
        [o.source, o.target] = [o.target, o.source];
        o.valueN *= -1;
    }
});

console.log(array);
.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

Sorting an array of objects by property values

Joining property values of objects in an array

Efficient way to swap bytes in python

An efficient way of removing objects from an indexedDB object store that are missing a property

Efficient way to merge array of objects with similar data

A more efficient way to rearrange Array of Objects?

Recursive array of property values in array of objects

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

What is the most efficient way of accessing a property from an array of objects in PowerShell?

Efficient way to see if a set of values appear in an array?

Javascript - Efficient way to get distinct values from an array of objects

Passing property values the efficient way

Efficient way to update values to array of hashes in ruby?

Most efficient way to search array of object values

More efficient way search for multiple objects in array

Restructuring JavaScript array of objects in the most efficient way

Is there a way to swap positions of 3 objects in an array?

Efficient way to swap array elements that are flipped

Efficient way to group objects in an Array

Most efficient way to get average by object property (array of Objects)

Efficient way of alternating sort of array of objects based on property

Most efficient way to update an object property within an array of objects

Efficient way to get unique objects from array of objects, by object property?

Javascript: Filter array of objects based on array values in an efficient way

Efficient way for mapping array values

Swift: Most efficient way to split array of objects into multiple arrays based on object property?

Most efficient way to use an array of objects to query more objects - MongoDb?

Efficient way to separate an array of objects based on another array

Is there an efficient way to get an array of objects from another array of objects