1

I have the following JSON data which comes from scraping the technology on a page, I have made a Json array called MyArray that is below:

{ url: 'http://www.ozautoelectrics.com',
originalUrl: 'http://www.ozautoelectrics.com',
applications:
[ { name: 'Font Awesome',
   confidence: '100',
   version: '',
   icon: 'Font Awesome.png',
   website: 'http://fontawesome.io',
   categories: [Object] },
 { name: 'Google Analytics',
   confidence: '100',
   version: '',
   icon: 'Google Analytics.svg',
   website: 'http://google.com/analytics',
   categories: [Object] },
 { name: 'jQuery',
   confidence: '100',
   version: '2.1.3',
   icon: 'jQuery.svg',
   website: 'http://jquery.com',
   categories: [Object] } ] }

My question is how do I access the [Object] within categories using NODE? or any of the other things inside applications?

I can use myArray.url to get the URL but how do I get whats inside applications properly? I tried myArray.applications.name

Also i'm new.

6
  • Possible duplicate of Access / process (nested) objects, arrays or JSON Commented Jul 2, 2017 at 6:19
  • @user3679330 Any specific reason you have removed my answer as accepted answer? Commented Jul 2, 2017 at 6:31
  • I tried to make them all the accepted answer, as they all did what I wanted Commented Jul 2, 2017 at 6:54
  • @user3679330 No you can't accept all the answers. you can accept only one that solved you problem. If you accept other answers means it will revert of already accepted answer. Commented Jul 2, 2017 at 6:56
  • You can have the accepted answer then, i feel like you wanted it more Commented Jul 2, 2017 at 6:58

3 Answers 3

1

Loop through applications and use index to access categories.

var myArray = { url: 'http://www.ozautoelectrics.com',
originalUrl: 'http://www.ozautoelectrics.com',
applications:
[ { name: 'Font Awesome',
   confidence: '100',
   version: '',
   icon: 'Font Awesome.png',
   website: 'http://fontawesome.io',
   categories: [Object] },
 { name: 'Google Analytics',
   confidence: '100',
   version: '',
   icon: 'Google Analytics.svg',
   website: 'http://google.com/analytics',
   categories: [Object] },
 { name: 'jQuery',
   confidence: '100',
   version: '2.1.3',
   icon: 'jQuery.svg',
   website: 'http://jquery.com',
   categories: [Object] } ] 
   };
   
   for(var i =0; i<myArray.applications.length;i++){
    console.log(myArray.applications[i].categories);
   }

Sign up to request clarification or add additional context in comments.

Comments

1

You can use forEach method to access each element of array.

var myArray = { 
url: 'http://www.ozautoelectrics.com',
originalUrl: 'http://www.ozautoelectrics.com',
applications: [ { name: 'Font Awesome',
   confidence: '100',
   version: '',
   icon: 'Font Awesome.png',
   website: 'http://fontawesome.io',
   categories: [Object] },
 { name: 'Google Analytics',
   confidence: '100',
   version: '',
   icon: 'Google Analytics.svg',
   website: 'http://google.com/analytics',
   categories: [Object] },
 { name: 'jQuery',
   confidence: '100',
   version: '2.1.3',
   icon: 'jQuery.svg',
   website: 'http://jquery.com',
   categories: [Object] } 
   ]};
   
myArray.applications.forEach(function(value, index, array){
  console.log(value.categories); //Allow to access the array
  //console.log(value.categories[0]); //Allows the first element in the array
});


console.log('Arrow function Solution');
//solution with arrow function
myArray.applications.forEach((value, index, array) => {
  console.log(value.categories); //Allow to access the array
  //console.log(value.categories[0]); //Allows the first element in the array
});

Comments

1

You can use lodash map function for that. https://lodash.com/docs/4.17.4#map

var lodash = require('lodash');
var categoriesArr = lodash.map(myArray.applications, (item) => item.categories);

2 Comments

how would you write this without the => ?
just use regular function, lodash.map(myArray.applications, function(item) {return item.categories});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.