[英]Get user input from HTML and search JSON array for value
Let´s say I have a Json File which looks like this:假设我有一个 Json 文件,如下所示:
{"403": [ { "403-01-01": "219,00" }, { "403-01-02": "180,00" } ],
"404": [ { "404-01-01": "26,00" }, {"403-01-02": "2612,00"} ] }
the file is much bigger and probably when finished has like 400+ Entries文件要大得多,完成后可能有 400 多个条目
and i want a user to input something on a website like 403-01-02
how would i be able to get the input into a js value and search the array for the value of the entry and display it to the user.我希望用户在403-01-02
类的网站上输入一些内容,我如何能够将输入转换为 js 值并在数组中搜索条目的值并将其显示给用户。
maybe having each new set of starting codes (
403
,404
...etc) as a new array is a dumb idea and it does not have to be that way but i think it looks better when viewed so i chose to format it like this first也许将每组新的起始代码(403
404
等)作为一个新数组是一个愚蠢的想法,它不一定是那样的,但我认为它看起来更好,所以我选择像这样格式化它第一的
Can this help you?这可以帮助你吗? This code log the value of the entry given to userRequest
此代码记录给予userRequest
的条目的值
let array = {
"403": [ { "403-01-01": "219,00" }, { "403-01-02": "180,00" } ],
"404": [ { "404-01-01": "26,00" }, {"403-01-02": "2612,00"} ]
};
let userRequest = "403-01-02";
let arrayEntry = userRequest.split('-')[0];
let requestValue = array[arrayEntry].filter(function(el) {
return userRequest in el;
});
console.log(requestValue[0][userRequest]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.