在数组javascript中的对象中删除对象 [英] removing objects in an object in an array javascript

查看:90
本文介绍了在数组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

使用mathjs,我能够评估表达式,并使用array.push命令将值动态添加到数组中并显示它们。但是,一旦虚值出现在数组结果中,我的代码就会中断。

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.

如何从数组中删除这些虚数?换句话说,在将值开始显示之前,我需要删除它们的 im:部分。

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.

我尝试使用从以前对别人的问题的答案中找到的一些代码来完成此操作(如何从JavaScript中的数组中删除特定元素?)这样的拼接命令:

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

,但它在控制台中返回未定义im。

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

非常感谢任何帮助!

推荐答案

您可以使用数组 map 函数。
基本上,我们遍历数组。如果该商品具有 .re 属性,则仅采用该值。如果没有 .re 属性,我们将该值保持不变。

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.

我们可以用简写形式表示,例如使用三元运算符和箭头功能的 result ,或者我们可以使用稍微更冗长但传统的方式编写它,例如使用 resultTwo

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

这篇关于在数组javascript中的对象中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆