Chris Phillips
Chris Phillips

Reputation: 71

How to filter JSON Object (JavaScript)

I am trying to filter the below JSON object and return an array of objects where the value within the key of 'markdown' contains say "steve" - to do this I suspect I need to convert the object to an array then filter.

I have tried using Object.entries

Object.entries(diagnosisData).filter(item => item === 'steve')

as well as JSON.parse but think I am barking up the wrong tree.

I'd like to return say:

result = [
  {
    "id": "stevey",
    "markdown": "STEVEY",
    "source": null
  },
  {
    "id": "steven",
    "markdown": "STEVEN",
    "source": null
  }
]

Could anyone offer a pointer for me?

Many thanks

JSONdata = {
  "steven": {
    "id": "steven",
    "markdown": "STEVEN",
    "source": null
  },
  "john": {
    "id": "johnA",
    "markdown": "JOHNA",
    "source": null
  },
  "henry": {
    "id": "henryP",
    "markdown": "HENRYP",
    "source": null
  },
  "stevel": {
    "id": "steveL",
    "markdown": "STEVEL",
    "source": null
  }
}

Upvotes: 5

Views: 15848

Answers (3)

StepUp
StepUp

Reputation: 38209

Try to use Object.entries:

let filterKey = 'steve';
const result = Object.entries(JSONdata).filter(([k, v]) => k== filterKey);

and then Object.fromEntries() to create an object from a list of key-value pairs:

Object.fromEntries(result)

An example:

let JSONdata = {
   "steve": {
     "id": "steve",
     "markdown": "STEVE",
     "source": null
   },
   "john": {
     "id": "john",
     "markdown": "JOHN",
     "source": null
   },
   "henry": {
     "id": "henry",
     "markdown": "HENRY",
     "source": null
   },
 };

 let filterKey = 'steve';
 const result = Object.entries(JSONdata).filter(([k, v]) => k == filterKey);
 console.log(Object.fromEntries(result))

UPDATE:

You can use startsWith method to get desired result:

const result = Object.values(JSONdata).filter((v) => 
     v.id.startsWith(filterKey));

An example:

let JSONdata = {
   "steveN": {
     "id": "steven",
     "markdown": "STEVEN",
     "source": null
   },
   "john": {
     "id": "johnA",
     "markdown": "JOHNA",
     "source": null
   },
   "henry": {
     "id": "henryP",
     "markdown": "HENRYP",
     "source": null
   },
   "steveL": {
     "id": "steveL",
     "markdown": "STEVEL",
     "source": null
   }
 }

 let filterKey = 'steve';
 const result = Object.values(JSONdata).filter((v) => 
     v.id.startsWith(filterKey));
 console.log(result)

Upvotes: 1

CherryDT
CherryDT

Reputation: 29092

You are almost there with your Object.entries(...).filter approach. However, I'm not sure what you want to filter by - the keys, the id values or the markdown values?

To avoid confusion, let's say you have an object like this instead:

const data = {
  keyA: {
    id: 'idA',
    markdown: 'markdownA'
  },
  keyB: {
    id: 'idB',
    markdown: 'markdownB'
  }
}

Then, just for reference, the Object.XX functions yield these results:

console.log(Object.keys(data))
// ['keyA', 'keyB']

console.log(Object.values(data))
// [
//   {id: 'idA', markdown: 'markdownA'},
//   {id: 'idB', markdown: 'markdownB'}
// ]

console.log(Object.entries(data))
// [
//   ['keyA', {id: 'idA', markdown: 'markdownA'}],
//   ['keyB', {id: 'idB', markdown: 'markdownB'}]
// ]

So:

To filter by the key, there is no filter needed at all as long as it's a perfect match your are looking for:

const result = data.keyA
console.log(result) // {id: 'idA', markdown: 'markdownA'}

If needed a non-exact match though, say for example all keys ending with A, you can use Object.entries(...).filter (and then map to the value):

const result = Object.entries(data)
  .filter(([key, value]) => key.endsWith('A'))
  .map(([key, value]) => value)
console.log(result) // [{id: 'idA', markdown: 'markdownA'}]

To filter by one of the properties (id or markdown), you could use Object.entries(...).filter, but since you are then not even interested in the key, you could use Object.values(...).filter instead:

const result = Object.values(data).filter(item => item.id === 'keyA')
console.log(result) // [{id: 'idA', markdown: 'markdownA'}]

Upvotes: 6

Simon
Simon

Reputation: 6490

One way to do it is: Object.values(JSONdata).filter(o => o.id === 'steve')

Or if you do not have the is and want to do it by key:

const key = Object.keys(JSONdata).filter(o => o === 'steve');

console.log(JSONdata[key]);

Upvotes: 0

Related Questions