简体   繁体   English

Javascript 数组继承 object 的键

[英]Javascript array inherit keys of object

I'm used to program on PowerShell and fairly new to JavaScript. In PowerShell you can create an array of custom objects like that: You have an object with 3 properties of value 1,2,3 for $item1:我习惯于在 PowerShell 上编程,对 JavaScript 还是个新手。在 PowerShell 中,您可以创建一个自定义对象数组:您有一个 object,其中 $item1 的 3 个属性值为 1、2、3:

Property1 Property2 Property3
--------- --------- ---------
        1         2         3

Same properties with values 10,20,30 for $item2 $item2 的相同属性值为 10、20、30

Property1 Property2 Property3
--------- --------- ---------
       10        20        30

And if you add them to an arraylist it automatically inherits their properties:如果将它们添加到 arraylist 中,它会自动继承它们的属性:

$collectionVariable = New-Object System.Collections.ArrayList
$collectionVariable.Add($item) | Out-Null
$collectionVariable.Add($item2) | Out-Null
$collectionVariable

Property1 Property2 Property3
--------- --------- ---------
        1         2         3
       10        20        30

I have tried to do the same in JavaScript but ultimately failed as each added object appears as a new key.我曾尝试在 JavaScript 中做同样的事情,但最终失败了,因为每个添加的 object 都显示为新密钥。

var arraylist = new Array();
arraylist.push(item);

It seems that it does not work that way as the arraylist does not inherit item's keys.它似乎不能那样工作,因为 arraylist 不继承项目的密钥。

How can I do so that my arraylist inherit the keys of my objects?我该怎么做才能让我的 arraylist 继承我的对象的键?

Make items into objects and push to the array:将项目变成对象并推送到数组:

const item = {
   prop1: 1,
   prop2: 2,
   prop3: 3
}

arr.push(item);

If you want your keys of object to be elements of the array list : Do this,如果您希望object成为数组列表元素:执行此操作,

 const item = { prop1: 1, prop2: 2, prop3: 3 } let arr = Object.keys(item); console.log(arr)

If you want, values of object to be elements of array list , do this:如果您希望object成为数组列表元素,请执行以下操作:

 const item = { prop1: 1, prop2: 2, prop3: 3 } let arr = Object.values(item) console.log(arr)

ArrayList is a rather dumb (and IMO very much outdated) collection type. ArrayList是一个相当愚蠢的(和 IMO 非常过时的)集合类型。 It only supports the .NET type Object , and it certainly does not inherit anything.它只支持 .NET 类型Object ,它当然不会继承任何东西。

PowerShell on the other hand has smart logic to see beyond the stored objects and to print them using their properties, and to group on those properties if they are the same.另一方面, PowerShell具有智能逻辑,可以超越存储的对象并使用它们的属性打印它们,如果它们相同,则对这些属性进行分组。 What you see printed is because PowerShell is smart, not because ArrayList is smart (on the contrary).你看到打印的是因为 PowerShell 是智能的,而不是因为 ArrayList 是智能的(相反)。

Javascript has a similarly smart printing function, and that is console.table : Javascript有一个类似的智能打印 function,那就是console.table

 var items = []; items.push({ prop1: 1, prop2: 2, prop3: 3 }); items.push({ prop1: 10, prop2: 20, prop3: 30 }); console.table(items); // Run, then do F12, then look in "Console" tab

To see the results you need to Run the snippet, then press F12 to open the Developer Tools , then look in the "Console" tab to see the output. Here is a screenshot of it:要查看运行代码片段所需的结果,然后按F12打开开发人员工具,然后在“控制台”选项卡中查看 output。这是它的屏幕截图:

控制台输出的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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