简体   繁体   English

在带有数组的对象上使用它时,为什么会出现$ parse错误?

[英]Why I am getting $parse error when use it on object with array?

I have two json files. 我有两个json文件。 I am getting them like this: 我正在这样得到他们:

var fr, en;
$.getJSON('fr.json', function(data) {
    fr = data;
}).complete(function() {
    ...
});

$.getJSON('en.json', function(data) {
    en = data;
}).complete(function() {
    ...
});

This files are locale files. 该文件是语言环境文件。 I want to test them. 我想测试一下。 I want to compare objects fr and en by property names, so unit test will show, when something is missing in locale file. 我想通过属性名称比较对象fr和en,以便在语言环境文件中缺少某些内容时显示单元测试。

To get what properties are missing in EN locale I am using this algorithm: 为了获得EN语言环境中缺少的属性,我正在使用以下算法:

  1. Get all object paths, like abc, abe, adx etc in FR locale 在FR语言环境中获取所有对象路径,例如abc,abe,adx等
  2. Then I use $parse(*path*)(fr); 然后我使用$parse(*path*)(fr); and if it returns something -> EN locale has same property as FR locale has. 并返回-> EN语言环境与FR语言环境相同的属性。

So my problem. 所以我的问题。 I have such string in file fr. 我在文件fr中有这样的字符串。 "some": { "numbers": ["one", "two", "three"] ... } In first step, I got such property paths: “ some”:{“数字”:[“一个”,“两个”,“三个”] ...}在第一步中,我得到了这样的属性路径:

some.numbers.0
some.numbers.1
some.numbers.2

And when I am trying to $parse this paths on EN locale I got this error: 当我尝试在EN语言环境中解析此路径时,出现此错误:

Error: [$parse:syntax] Syntax Error: Token '.0' is an unexpected token at column 15 of the expression [addForm.messages.numbers.0] starting at [.0].
http://errors.angularjs.org/1.4.3/$parse/syntax?p0=.0&p1=is%20an%20unexpected%20token&p2=25&p3=some.numbers.0&p4=.0

How can I get working $parse on objects, which contain arrays? 如何在包含数组的对象上使用$ parse? Or is there any other way to do what I need? 还是有其他方法可以满足我的需求?

As @charlietfl mentioned in his comment to the question: 正如@charlietfl在对问题的评论中提到的那样:

Object doesn't have those properties, numbers is an array. 对象没有那些属性,数字是一个数组。 Need to use proper javascript object/array notation 需要使用正确的javascript对象/数组表示法

So, I need use 所以,我需要使用

some.numbers[0]
some.numbers[1]
etc 

instead of 代替

some.numbers.0

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

相关问题 为什么在创建对象数组时出现错误? - Why am I getting an error when creating an array of objects? 为什么在使用parse.com对数组进行fetchAll时出现错误? - Why am I getting an error while doing fetchAll for an array with parse.com? "为什么这个多维数组出现错误?" - Why am I getting an error with this multidimensional array? 尝试在简单对象上执行JSON.parse时出现“意外令牌”错误 - I am getting an “Unexpected token” error when trying to do JSON.parse on a simple object 为什么在JS数组上调用.toString时会出现“ object Object”? - why am I getting “object Object” when I call .toString on a JS array? 尝试在Ionic中使用列表时,为什么会出现此JavaScript错误? - Why am I getting this JavaScript error when I try use lists in Ionic? 为什么我将同一个对象放入数组? - Why am I getting the same object pushed into my array? 为什么我在访问 object 中的数组时得到 undefined? - Why am I getting undefined while accessing an array inside an object? 包括Shopify链接产品选项时,为什么会收到此错误“ TypeError undefined is an object…”? - Why am I getting this error 'TypeError undefined is not an object…' when including Shopify Linked Product Options? 为什么当我生成错误时我收到字符串错误? - Why when i generate an Error i am getting an String Error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM