关联数组元素与对象字段相同吗?

发布于 2024-10-14 17:58:13 字数 663 浏览 2 评论 0原文

我有一个看起来像这样的物体。

var obj = {
    speed: 10,
    volume: 14,
    aux_deform: 1.8,
    energy: 21.5,
    aux_energy: 0.2
}

在运行时将添加其他字段/属性,因此该对象可以在任何给定时间包含任意数量的属性。正如您在我的对象示例中看到的,某些属性可以以“aux_”开头。

现在,我想从此对象创建另一个对象,该对象仅包含以“aux_”开头的属性。

我现在的做法是这样的。

var newObj = [];
for(prop in obj)
{
    if (prop.startsWith('aux_'))
    {
        newObj[prop] = obj[prop];
    }
}

现在我有了一个新对象,我可以执行以下操作:

alert(newObj.aux_energy);

这可以正常工作,但我想知道这是否是正确的方法。对于我想要实现的目标,有没有更简单的方法或更优雅的方法?这种创建对象的方式是否存在任何(安全或技术)问题?

标题中的问题是:新创建的对象实际上是一个以属性为键的关联数组(因为它是这样创建的)还是有什么区别。

I have an object that looks something like this.

var obj = {
    speed: 10,
    volume: 14,
    aux_deform: 1.8,
    energy: 21.5,
    aux_energy: 0.2
}

There will be additional fields/properties added at runtime, so the object can contain any number of properties at any given time. As you can see in my object example, some properties can start with "aux_".

Now, I want to create another object from this one that contains only properties that start with "aux_".

The way I am doing it now is something like this.

var newObj = [];
for(prop in obj)
{
    if (prop.startsWith('aux_'))
    {
        newObj[prop] = obj[prop];
    }
}

Now I have a new object and I can do something like:

alert(newObj.aux_energy);

This works ok, but I am wondering if this is the right approach. Is there any easier way, or a more elegant way for what I am trying to achieve? Are there any (security or technical) issues/problems with this way of creating objects?

And the question from the title: is the newly created object actually an associative array with properties as keys (since it's created that way) or is there any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2024-10-21 17:58:13

JavaScript 中没有关联数组,只有 JSON 对象可以用来代替关联数组。您在这里所做的是声明一个名为 newObj 的数组,然后向该数组添加属性。当尝试在数组方法中使用 buit 或对其进行迭代时,可能会导致意外行为。 JSON 对象和数组之间的主要区别在于对象中的属性不可排序,并且迭代它们可能会产生随机且不可预测的输出。

更合适的编写方式是声明一个对象 newObj = {},然后复制您需要的属性。

需要注意的一件事 - 如果您将数组或对象作为属性的值,您将复制对这些对象的引用。如果您希望能够独立于原始对象编辑这些值,则必须深度复制这些值 - 检查此问题以了解如何操作 在 JavaScript 中深度克隆对象的最有效方法是什么?

除此之外,这是唯一的方法这样做就完全没问题了。

There are no associative arrays in Java Script, there are only JSON objects which can be used in place of associative arrays. What you did here is that you have declared an array called newObj and then added properties to this array. It could lead to unexpected behaviors when trying to use buit in Array methods or when iterating over it. The main difference between JSON objects and arrays is that properties in objects aren't sortable and iterating over them can produce random and unpredictable output.

More appropriate way to write that would be to declare an object newObj = {} and then just copy the properties you need.

One thing to note - if you will have arrays or objects as values for properties you will copy references to these objects. If you'd like to be able to edit these values independently from the original object you'd have to deep copy these values - check this question for how to on that What is the most efficient way to deep clone an object in JavaScript?

Apart from that it's the only way to do that and it's perfectly fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文