Navigate back to the homepage

How to get all values from nested json object in javascript?

Infinitbility
JavaScript
May 18th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, we will see how can we get all values from nested JSON objects, here we will use a custom function which will return the list of values available in a nested JSON object.

So, we will create a function that navigates every possible object’s key and value and add values in an array that returns at the end of recursive loops.

Well, let’s create a function that returns the values of nested objects.

1const getNestedValues = (data, values) => {
2 if(!(data instanceof Array) && typeof data == 'object'){
3 Object.values(data).forEach( value => {
4 if(typeof value === 'object' && !(value instanceof Array)){
5 getNestedValues(value, values);
6 } else {
7 values.push(value);
8 }
9 });
10 }
11 return values
12}

In javascript when we check typeof of the array it will return i.e we use instanceof Array to handle array types of object.

Let’s take an example below the nested object.

1const data = {
2 person: {
3 male: {
4 name: "infinitbility"
5 },
6 female: {
7 name: "aguidehub"
8 }
9 }
10};
11
12const getNestedValues = (data, values) => {
13 if(!(data instanceof Array) && typeof data == 'object'){
14 Object.values(data).forEach( value => {
15 if(typeof value === 'object' && !(value instanceof Array)){
16 getNestedValues(value, values);
17 } else {
18 values.push(value);
19 }
20 });
21 }
22 return values
23}
24
25getNestedValues(data, []);
26// [ 'infinitbility', 'aguidehub' ]

When you run the above code, you will get [ 'infinitbility', 'aguidehub' ] let’s check the output.

JavaScript, Get all values from nested json object example
JavaScript, Get all values from nested json object example

Want to reomove duplicate, read below tutorial.

https://infinitbility.com/how-to-remove-duplicates-from-array-of-strings-in-javascript

All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get count of json object in javascript?

Example of get count of json object in javascript with step by step guide.

May 18th, 2022 · 1 min read

How to remove square brackets from json object in javascript?

Example of remove square brackets from json object in javascript with step by step guide.

May 18th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]