如何检查值已存在于现有的JSON文件Node JS中(How to check value is already there in an existing JSON file Node JS)

编程入门 行业动态 更新时间:2024-10-26 04:27:05
如何检查值已存在于现有的JSON文件Node JS中(How to check value is already there in an existing JSON file Node JS)

我有一个在后端端的地址的json文件。从客户端,我通过API获取用户的地址作为post方法。 我想检查用户输入的地址是否已经存在于JSON文件中。 在json文件中的地址已经是小写的合成。 我是Node和JS的新手。 这是我试过的。 和我得到错误“消息”:“address.equals不是一个函数”

const addressesAsJson = require('../data/addresslist.json') function addressCheck ( address /* : string | void */ ) /* :Promise<Object[]> */ { var convertAdLowerCase = address.toLowerCase() return Promise.resolve() .then(() => { console.log(address) return addressesAsJson.filter((address) => address.equals(convertAdLowerCase)) console.log(address.equals(convertAdLowerCase)) }) }

我的JSON格式看起来像这样

{"address":"new south park , western sydney , australia 2345","display":"New South Park , WESTERN SYDNEY , AUSTRALIA 2345"}

I have a json file with addresses in backend side.. From client side I'm getting an address of user as a post method through API. I want to check whether the address entered by user is already exist in JSON file or not. In json file address are already as lowercase formate. I'm tottally new to Node and JS. This is what I tried. and I'm getting error "message": "address.equals is not a function"

const addressesAsJson = require('../data/addresslist.json') function addressCheck ( address /* : string | void */ ) /* :Promise<Object[]> */ { var convertAdLowerCase = address.toLowerCase() return Promise.resolve() .then(() => { console.log(address) return addressesAsJson.filter((address) => address.equals(convertAdLowerCase)) console.log(address.equals(convertAdLowerCase)) }) }

My JSON format look like this

{"address":"new south park , western sydney , australia 2345","display":"New South Park , WESTERN SYDNEY , AUSTRALIA 2345"}

最满意答案

使用相同的运算符

address === convertAdLowerCase

并删除返回后的控制台日志,因为它是无法访问的代码。

完美的代码将是

return Promise.resolve() .then(() => { console.log(address); const allMatches = addressesAsJson.filter((record) => record.address === convertAdLowerCase)); console.log(allMatches); return allMatches.length > 0; });

Use equal operator

address === convertAdLowerCase

and remove the console log which is after the return since it is unreachable code.

Perfect code would be

return Promise.resolve() .then(() => { console.log(address); const allMatches = addressesAsJson.filter((record) => record.address === convertAdLowerCase)); console.log(allMatches); return allMatches.length > 0; });

更多推荐

本文发布于:2023-08-07 18:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465510.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Node   值已存   JSON   existing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!