0

I've done some research on this issue. I am trying to manipulate an array of calculated values that looks like this in the console:

{nodeVoltages: Array(11), totalPower: Array(1), xlength: Array(11)}
  nodeVoltages: Array(11)
    0:48
    1:47.71306060387108
    2:47.250273223993105
    3:46.59686907269243
    4:45.71876416434013
    5:44.53304242029258
    6:42.745236969423615
    7:Complex {re: 40.38334500994142, im:1.919295696316476, __ember1513267958317: "ember368"}
    8:Complex { re:39.55961661806138, im:3.8933604519196416, __ember1513267958317: "ember369"}

This array is created dynamically through some math that I've come up with so there is no input data that I can give you. I'm trying to make the above array look like this:

{nodeVoltages: Array(11), totalPower: Array(1), xlength: Array(11)}
  nodeVoltages: Array(11)
    0:48
    1:47.71306060387108
    2:47.250273223993105
    3:46.59686907269243
    4:45.71876416434013
    5:44.53304242029258
    6:42.745236969423615
    7:40.38334500994142
    8:39.55961661806138

Using mathjs, I was able to evaluate my expressions and dynamically add the values into an array with the array.push command and display them. However, my code breaks once the imaginary values pop up in the results of my array.

How can I remove these imaginary numbers from my array? In other words, I need to remove the "im:" parts of the values when they begin to appear before I push them to the displayed array.

I tried to do this with some code I found from a previous answer to someone else's question (How do I remove a particular element from an array in JavaScript?) splice command like this:

      var nodeVoltage2 = parser.eval(expression2);

//checks if there are imaginary values and removes them

            if ("im" in nodeVoltage2) {
              nodeVoltage2.splice(2,1)
            }

//adds value to result array for analysis

      nodeVoltages.push(nodeVoltage2);

but it returns in the console that "im is not defined".

Any help is greatly appreciated!

Sebastian Brosch
  • 37,059
  • 14
  • 61
  • 73
  • 1
    Please share your input data in JSON format and expected output. – Hassan Imam Dec 14 '17 at 16:55
  • You're trying to remove "properties" of the objects, and you need to address their keys as strings (`'im'`). – isherwood Dec 14 '17 at 16:56
  • In this case, `im` is not defined. You never define it anywhere, yet you are calling a comparison to it. You could do something like `if "im" in nodeVoltage2`, which would check if nodeVoltage2 has a key named `im` – user3483203 Dec 14 '17 at 16:56
  • chris, you're right. The problem is still occurring after that fix, however the error is a little more helpful. It is still getting hung up at the first value since "im" is not defined. Do I just define it as a key in the "nodeVoltages" object from the start? – Christian Di Marco Dec 14 '17 at 17:02

1 Answers1

0

You can use the array map function. Basically, we loop through the array. If the item has a .re property, we take that value only. If there is no .re property, we keep the value as is.

We can either write that in shorthand, as with result using the ternary operator and arrow function, or we can write it in a slightly more verbose but traditional way, as with resultTwo

let data = [
    48
    ,47.71306060387108
    ,47.250273223993105
    ,46.59686907269243
    ,45.71876416434013
    ,44.53304242029258
    ,42.745236969423615
    ,{re: 40.38334500994142, im:1.919295696316476, __ember1513267958317: "ember368"}
    ,{ re:39.55961661806138, im:3.8933604519196416, __ember1513267958317: "ember369"}
]

let result = data.map((x) => x && x.re ? x.re : x);


let resultTwo = data.map(function(elem) {
    // First, we need to check that the array element is not null / undefined
    // We then need to check that it has a property called re that is also not null / undefined
    if (elem != null && elem.re != null) {
        // Just return the property we're interested in
        return elem.re;
    } else {
        // Return the element as is
        return elem;
    }
});

console.log(result);
console.log(resultTwo);
user184994
  • 15,680
  • 1
  • 33
  • 45
  • That's exactly what I needed. Thank you. I'm not familiar with the code inside the data.map method. Could you explain what that is? – Christian Di Marco Dec 14 '17 at 17:39
  • Absolutely, I've updated my answer and added resultTwo, which is doing exactly the same thing, but written in a less shorthand way, with comments to explain what it's doing – user184994 Dec 14 '17 at 17:47
  • I tried doing this with a different set of values (values for power of the system and not the voltages) and it returns complex values with "NaN" as the real part. I thought maybe that was coming up because I'm using big numbers but all my other values go to ~15 decimal places. Is there any way I could incorporate this method of checking to only display "NaN" when that happens? – Christian Di Marco Dec 15 '17 at 19:31
  • posted: https://stackoverflow.com/questions/47839035/only-display-real-value-from-complex-array-javascript – Christian Di Marco Dec 15 '17 at 19:52