venura
venura

Reputation: 125

How to check value is already there in an existing JSON file Node JS

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

Upvotes: 0

Views: 82

Answers (2)

prabushitha
prabushitha

Reputation: 1483

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;
 });

Upvotes: 1

IftekharDani
IftekharDani

Reputation: 3729

Plz refer link

var a = {"address":"new south park , western sydney , australia 2345","display":"New South Park , WESTERN SYDNEY , AUSTRALIA 2345"};
var b = {"address":"New south Park , western sydney , australia 2345","display":"New South Park , WESTERN SYDNEY , AUSTRALIA 2345"}
function jsonEqual(a,b) {
    return JSON.stringify(a).toLowerCase() === JSON.stringify(b).toLowerCase();
}
console.log(jsonEqual(a, b))

Upvotes: 0

Related Questions