简体   繁体   中英

removing objects in an object in an array javascript

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!

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM