[英]expected an identifier and instead saw an expression
Hi,你好,
I have this code:我有这个代码:
obj = {"ignoredid":"3329"},{"ignoredid":"19693"};
hit = "3329";
if (Object.values(obj).indexOf(hit) > -1) {
console.log('has it');
} else {console.log('doesnt');}
but I get a warning that says: expected an identifier and instead saw an expression
但我收到一条警告说:
expected an identifier and instead saw an expression
Why is that?这是为什么? This is the fiddle
这是小提琴
https://jsfiddle.net/o7u5L90h/1/ https://jsfiddle.net/o7u5L90h/1/
the code works fine on the fiddle despite the warning but not on my actual project (there it will always output "doesnt" no matter what) so I wonder if it will work once I get rid of the warning.尽管有警告,但代码在小提琴上运行良好,但在我的实际项目中却没有(无论如何它总是输出“doesnt”)所以我想知道一旦我摆脱了警告它是否会起作用。 What am I missing here?
我在这里缺少什么?
Thank you.谢谢你。
Maybe you want array of objects?也许你想要对象数组? Then you can do something like this
然后你可以做这样的事情
// Helper function to check array of objects const hasIt = (obj, val) => { for(const v of obj) if(v['ignoredid'] === val) return true; return false; } // Define array of objects const obj = [{"ignoredid":"3329"},{"ignoredid":"19693"}]; // Test it if(hasIt(obj, "3329")) console.log('has it'); else console.log('doesnt');
Or with more recent approach或者用最近的方法
// Define array of objects const obj = [{"ignoredid":"3329"},{"ignoredid":"19693"}]; // Test it if(obj.find(o => o.ignoredid === '3329')) console.log('has it'); else console.log('doesnt');
To answer your real question, what is happening there is that the statement obj = {ignoredid : 3329},{ignoredid : 19693};
要回答您真正的问题,发生的事情是语句
obj = {ignoredid : 3329},{ignoredid : 19693};
does not cause a syntax error but does not do what you expect it to do.不会导致语法错误,但不会执行您期望的操作。
Though it's not clear what you expected to happen 😁.虽然不清楚你期望发生什么😁。 That's why a lint is warning that
expected an identifier and instead saw an expression
这就是为什么 lint 警告
expected an identifier and instead saw an expression
It's being interpreted as something close to the following:它被解释为接近以下内容:
obj = {ignoredid : 3329}, {ignoredid : 19693}; console.log(JSON.stringify(obj)); // Is interpreted as obj = {ignoredid : 3329} {ignoredid : 19693} console.log(JSON.stringify(obj)); // Or ((obj = {ignoredid : 3329}), {ignoredid : 19693}); console.log(JSON.stringify(obj))
In both cases, the literal {ignoredid : 19693}
is being discarded and not used by the program.在这两种情况下,文字
{ignoredid : 19693}
都被丢弃并且不被程序使用。
Note that if you started writing code that was using const/let
or even var, you wouldn't have this problem and you'd be told you have a real syntax error and you wouldn't get this weird behavior.请注意,如果您开始编写使用
const/let
甚至 var 的代码,则不会遇到此问题,并且会被告知您有真正的语法错误,并且不会出现这种奇怪的行为。 That is because a VariableDeclaration
is not an Expression
, and expressions separated by commas is what's happening here.这是因为
VariableDeclaration
不是Expression
,这里发生的事情是用逗号分隔的表达式。 See the end of the answer for further details.有关更多详细信息,请参阅答案的末尾。
Uncaught SyntaxError: Invalid destructuring assignment
未捕获的语法错误:无效的解构赋值
const obj = {ignoredid : 3329}, {ignoredid : 19693}; console.log(JSON.stringify(obj));
If you intended it as obj = {ignoredid : 3329, ignoredid : 19693};
如果你打算将它作为
obj = {ignoredid : 3329, ignoredid : 19693};
, that would mean that the obj
would be {ignoredid : 19693}
, that is, the last property with the same name wins out. ,这意味着
obj
将是{ignoredid : 19693}
,也就是说,具有相同名称的最后一个属性胜出。
This is why you were always getting console.log('doesnt')
, your object did not contain a value of 3329
only 19693
;这就是为什么你总是得到
console.log('doesnt')
,你的对象不包含3329
只有19693
的值;
As suggested by tark , Umitigate and me, you should use an array.正如建议TARK ,Umitigate和我,你应该使用数组。 I just wanted to answer the real question since the behavior is bit odd even outside of your quirky declaration😜
我只是想回答真正的问题,因为即使在你古怪的声明之外,这种行为也有点奇怪😜
If you analyze the syntax tree of your example , you'll notice that the body of the program is an ExpressionStatement
but it does not contain an assignment directly, instead, it contains a nested SequenceExpression
which contains a separate AssignmentExpression
and an ObjectExpression
which is not what you intended如果您分析示例的语法树,您会注意到程序的主体是一个
ExpressionStatement
但它不直接包含赋值,而是包含一个嵌套的SequenceExpression
,其中包含一个单独的AssignmentExpression
和一个ObjectExpression
,它不是你的意图
However, if you analyze the syntax tree of obj = {"ignoredid":"3329"};但是,如果分析obj = {"ignoredid":"3329"};的语法树; , you'll notice that there's an AssignmentExpression nested directly in the first
ExpressionStatement
of the program ,你会注意到在程序的第一个
ExpressionStatement
中直接嵌套了一个 AssignmentExpression
So the message is indicating that it expected an Assignment
but instead saw an Expression
所以该消息表明它期望一个
Assignment
但看到一个Expression
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.