0

Here's an example JSON file:

{
  "cat": {
    "eyeColor": "gold",
    "legCount": 4
  },
  "dog": {
    "eyeColor": "brown",
    "legCount": 4
  },
  "turtle": {
    "eyeColor": "black",
    "legCount": 4
  }
}

I'd really like to provide example code, but I really don't know how to do it, so I'll just create an example of what I'd like below, I'm really sorry for this.

What I'd like is to filter and get an object's name, how do I go about doing this?

For example, I'd like to filter where the eye colour is "gold", and then get the Object's name which would be "cat", how do I do this?

Thank you very much! >w<

Yazuki Rin
  • 113
  • 1
  • 12

3 Answers3

1

Is it this what you want? You pass the data, then you choose the property name you want to filter and then the value you searching for and it returns the property name back. Otherwise it returns false

let data = {
  "cat": {
    "eyeColor": "gold",
    "legCount": 4
  },
  "dog": {
    "eyeColor": "brown",
    "legCount": 4
  },
  "turtle": {
    "eyeColor": "black",
    "legCount": 4
  }
}

function filterData(object, property, value){
   for(let prop in object){
      if(object[prop][property] == value) return prop;
   }
   return false;
}

let result = filterData(data, "eyeColor", "gold")

console.log(result);

/*
Above method will work if there's only one matching object is available,
but then the below method will be helpful if there are more than one
matching objects are available 
*/

data = {
  "cat": {
    "eyeColor": "black",
    "legCount": 4
  },
  "dog": {
    "eyeColor": "brown",
    "legCount": 4
  },
  "turtle": {
    "eyeColor": "black",
    "legCount": 4
  }
}

function filterDataAll(object, property, value){
   let result = [];
   for(let prop in object){
    if(object[prop][property] == value) result.push(prop)
   }
   return result;
}

result = filterDataAll(data, "eyeColor", "black")

console.log(result);
Nithish
  • 5,393
  • 2
  • 9
  • 24
bill.gates
  • 14,145
  • 3
  • 19
  • 47
0

You would first need to read the json file and parse it using fs module, then store the resulting object, say as animals. This may help:
Using Node.JS, how do I read a JSON file into (server) memory?

What you exactly need to do is find those keys of object animals which satisfy the condition

animals[key].eyeColor == "gold" 

You can achieve the same using this code:

var result = Object.keys(animals).filter(key => animals[key].eyeColor === 'gold')
console.log(result)

result is an array of all keys which match the condition.

Kashinath Patekar
  • 1,083
  • 9
  • 23
-1

Does your mean is filtering some field and return target object?

const exampleObject = {
  "cat": {
    "eyeColor": "gold",
    "legCount": 4
  },
  "dog": {
    "eyeColor": "brown",
    "legCount": 4
  },
  "turtle": {
    "eyeColor": "black",
    "legCount": 4
  }
}
const result = Object
  .keys(exampleObject)
  .filter(k => exampleObject[k].eyeColor === 'gold')
  .map(k => exampleObject[k])

console.log(result)
lemon
  • 300
  • 1
  • 6