简体   繁体   中英

Get user input from HTML and search JSON array for value

Let´s say I have a Json File which looks like this:

{"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

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.

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

Can this help you? This code log the value of the entry given to 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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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