user3679330
user3679330

Reputation: 139

How to access Object in Array NODE JS

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.

Upvotes: 1

Views: 3845

Answers (3)

Lena Kaplan
Lena Kaplan

Reputation: 756

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

Upvotes: 1

Hassan Imam
Hassan Imam

Reputation: 22524

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

Upvotes: 1

Dinesh undefined
Dinesh undefined

Reputation: 5546

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

Upvotes: 1

Related Questions