简体   繁体   English

如何在对象文字分配中指定动态值?

[英]How can I specify dynamic value in object literal assignment?

This is what I got after running my code through JSLint: 这是通过JSLint运行代码后得到的:

Expected ':' and instead saw '+'. 预期为“:”,而不是“ +”。 '"Book":"'+bookname+'"' line 3 column 23 '“ Book”:“'+ bookname +'”'第3行第23列

Unexpected trailing space. 意外的尾随空间。 '"Book":"'+bookname+'"' '“ Book”:“'+ bookname +'”'

I want to specify in a JSON string to find results if they contain the value of "bookid" in the "Book" column (cf. lines 1-4 below). 我想在JSON字符串中指定查找结果,如果它们在“ Book”列中包含“ bookid”的值(请参阅下面的1-4行)。

I understand that there are errors with the syntax. 我了解语法有误。 It was working great until I decided I needed to use a value from local storage. 直到我决定需要使用本地存储中的值之前,它的工作都很棒。 I am unsure of how to properly call a regular variable in this kind of query. 我不确定如何在这种查询中正确调用常规变量。 What is wrong? 怎么了?

var bookname = localStorage.getItem('bookid');

var whereObject = {
    "$and": [{
        '"Book":"'+bookname + '"'
    }
    }, {
        "$or": [{
            "calf_id": {
                "$regex": "^" + value,
                "$options": "i"
            }
        }, {
            "cow_id": {
                "$regex": "^" + value,
                "$options": "i"
            }
        }, {
            "Sire": {
                "$regex": "^" + value,
                "$options": "i"
            }
        }, {
            "Color": {
                "$regex": "^" + value,
                "$options": "i"
            }
        }]
    }]
    };

return JSON.stringify(whereObject);

value is the value that is in the search bar. value是搜索栏中的值。

I tried to change it using the advice given me, but then it just produces more warnings, similar to the ones I already have. 我尝试使用给我的建议来更改它,但随后它会产生更多警告,类似于我已有的警告。

I basically need to know what the proper way to use a previously defined variable in a query. 我基本上需要知道在查询中使用先前定义的变量的正确方法。 I don't know the proper syntax and haven't been able to find anything only 我不知道正确的语法,只能找不到任何东西

'"Book":"'+bookname + '"'

why are you using ' 's? 为什么使用' i'm not sure why you are doing that. 我不确定为什么要这么做。
in json, you should end the string before the : charachter. 在json中,您应该在: charachter之前结束字符串。 and after the : you type the value for the Book . 然后在:您输入Book的值。

"KEY":"VALUE"

if value of Book is a string: 如果Book值是一个字符串:

"Book" : "hello this is string"

if the value of Book is a variable: 如果Book的值是一个变量:

"Book": bookname

if you want your Book to have a value of the bookname variable but enclosed in quotes, you do 如果您想让Book拥有bookname变量的值但用引号引起来,则可以

"Book": "'" + bookname + "'"

if you look carefully and count the number of { and }'s in your json you see the number is not even! 如果仔细查看并计算json中{和}的数目,您会发现该数目甚至不相等! which means you have a mistake matching those. 这意味着您在匹配这些错误。 here is the valid json without syntax error: 这是没有语法错误的有效json:

{
  "$and": [


       {
         "Book":"bookname"
       },


       {
         "$or": [
             {"calf_id": {"$regex":"^" + value,"$options": "i"}},
             {"cow_id":  {"$regex":"^" + value,"$options": "i"}},
             {"Sire":    {"$regex":"^" + value,"$options": "i"}},
             {"Color":   {"$regex":"^" + value,"$options": "i"}}
         ]
       }


  ]
}

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

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