简体   繁体   English

JavaScript对象如何包含属性(键值对)的数组(带有方括号)?

[英]How can a JavaScript object contain an array (with square brackets) of properties (key value pairs)?

I am working with some previous code in AngularJS, but my question pertains to a strange occurrence of the objects that are passed to the view. 我正在使用AngularJS中的一些先前的代码,但是我的问题与传递给视图的对象的奇怪出现有关。 I am getting an array of arrays. 我得到一个数组数组。 The nested arrays have key value pairs, however. 但是,嵌套数组具有键值对。 This doesn't make sense in my understanding. 以我的理解,这没有任何意义。

Additionally, I believe it is causing problems with the custom filters I am trying to make. 此外,我认为这会导致我尝试制作的自定义过滤器出现问题。 A general model filter works, but filtering on a specific field does not. 常规模型过滤器有效,但对特定字段的过滤则无效。 I have tested the concept in jsfiddle and it works fine if the data is formatted as an array of objects that have properties, but trying to replicate the structure of my current data output errors since it is not valid JavaScript. 我已经在jsfiddle中测试了该概念,并且如果将数据格式化为具有属性的对象数组,则可以正常工作,但是由于它不是有效的JavaScript,因此尝试复制当前数据输出错误的结构。 Can someone shed some light on my predicament? 有人可以阐明我的困境吗?

[Array[0], Array[0], Array[0]...]
0: Array[0]
  $$hashKey: "00U"
  firstGroupList: Object
  length: 0
  subTitle: "Capsules"
  title: "Esomeprazole Strontium"
  __proto__: Array[0]
1: Array[0]
  $$hashKey: "01X"
  firstGroupList: Object
  length: 0
  subTitle: "Tablets"
  title: "Salsalate"
  __proto__: Array[0]
2: Array[0]
3: Array[0]
4: Array[0]
...

How can an array like [title: "title", subTitle: "subTitle"] exist? [title: "title", subTitle: "subTitle"]这样的数组如何存在?

Consider the following: 考虑以下:

var arr = [1,2,3,4];
var obj = {key: 50, anotherKey: 70};

arr.prop = 'something-something';
obj.prop = 'something-something';

console.log(arr['prop']);
console.log(obj['prop']);

It will output "something-something" twice and internally very similar things are happening - you are setting a property of an object and then reading it. 它将输出两次“某物”,并且内部发生了非常相似的事情-您正在设置对象的属性,然后读取它。

However, the difference is the actual type of object - arr is of type Array and obj is of type Object. 但是,区别在于对象的实际类型-arr是Array类型,而obj是Object类型。 Please note that this is not the variable type we're talking about - both arr and obj are of type "object" (which you can easily check using typeof). 请注意,这不是我们要讨论的变量类型-arr和obj均为“对象”类型(您可以使用typeof轻松检查)。 The type of object actually refers to the object's prototype ie the object which that particular variable is based on (for those more familiar with OOP, this would be similar a class you inherited from, except in this case it was just one instance that was inherited from a class, not a whole new class). 对象的类型实际上是指对象的原型,即该特定变量所基于的对象(对于更熟悉OOP的对象,这类似于您从其继承的类,但在这种情况下,它只是被继承的一个实例来自一个班级,而不是一个全新的班级)。

Here's an article that may help you understand prototypes better: http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/ 这是一篇可以帮助您更好地理解原型的文章: http : //sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/

Going back to the original code, when would you actually notice the difference between arr and obj? 回到原始代码,您何时会真正注意到arr和obj之间的区别? Well, one thing that notably comes to mind is JSON: 好吧,值得注意的一件事就是JSON:

var strArr = JSON.stringify(arr); // [1,2,3,4]
var strObj = JSON.stringify(obj); // {"key":50,"anotherKey":70,"prop":"something-something"}

So JSON will "lose" the prop property in case you use it on an array - this is one example of why you should be careful when setting properties of an array. 因此,如果在数组上使用JSON,JSON将“丢失” prop属性-这是为什么在设置数组的属性时要格外小心的一个示例。

Finally, how would you iterate this array if you want to take the "prop" property into consideration as well as the "normal" array members? 最后,如果要考虑“ prop”属性和“普通”数组成员,如何迭代该数组? For this, you could use for..in: 为此,您可以使用for..in:

for (key in arr) console.log(arr[key]);
/* displays:
1
2
3
4
something-something
*/

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

相关问题 如果包含键值对,则返回数组中的对象 - Return object in array if contain key value pairs 方括号 Javascript Object 钥匙 - Square Brackets Javascript Object Key 如何在javascript中获取数组object中的键值对 - How to get key value pairs in array object in javascript 如何使用映射的键值对将数组转换为javascript中的对象? - How to convert an array into an object in javascript with mapped key-value pairs? 如何在 JavaScript 中将嵌套对象数组转换为具有键值对的对象 - How to convert an array of nested objects to an object with key, value pairs in JavaScript 如何在javascript中迭代数组中对象的键值对 - How to iterate the key value pairs of an object in an array in javascript Javascript:如何用键值对作为字符串从 object 创建数组? - Javascript: How to make array from object with key value pairs as strings? 如何将 Object {} 转换为 JavaScript 中键值对的数组 [] - How to convert an Object {} to an Array [] of key-value pairs in JavaScript 如何使用easyXDM通过AJAX帖子将javascript对象/数组作为键值对发送? - How can I send an javascript object/array as key-value pairs via an AJAX post with easyXDM? 访问带有混合键方括号的JavaScript对象 - Accessing javascript object with mixed key square brackets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM