Reputation: 125
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"
.
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
.
Upvotes: 2
Views: 1047
Reputation: 386624
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; }
Upvotes: 2
Reputation: 12874
array.forEach(x=> {
if(x.valueN < 0){
[x.source, x.target] = [x.target, x.source] //swapping values between two var
x.valueN *= -1;
}
});
You can try the above code. Loop thru the array and if found negative values, swap the variable and negate the valueN
Upvotes: 1
Reputation: 6366
Mapping the array with a call to parseFloat and Math.abs seems the simplest solution:
var data = [{
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"
}
];
//Fix data by remapping values
data.forEach(function (entry) {
var floatingValue = parseFloat(entry.valueN);
entry.valueN = Math.abs(floatingValue);
if (floatingValue < 0) {
//Flipping
var temp = entry.source;
entry.source = entry.target;
entry.target = temp;
}
});
console.log(data);
Upvotes: 2
Reputation: 36620
Here is a solution that works:
let arr = [
{
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"
}
]
for (let el of arr) {
if(parseInt(el.valueN) > 0) {
continue;
}
let source = el.source;
let target = el.target;
el.target = source;
el.source = target;
el.valueN = (el.valueN * -1).toString();
}
console.log(arr);
Hopefully you find this helpful. If you do not understand a specific part you can further ask me about it.
Upvotes: 1