How to access Object in Array NODE JS - arrays

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.

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

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

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

Related

Changed AngularJS value from method does not persist after refresh

I am trying to modify the value of a module value in AngularJS but it does not persist. If I click around the app after its modified its fine but the moment I refresh the page it get returned to the default value. How can I change the value and have it persist while the app is being used.
Here is the value:
;(function() {
angular.module('app.common')
.value('config', {
date: {
dateFormat: 'YYYY-MM-DD',
angularDateFormat: 'yyyy-MM-d',
d3DateFormat: '%Y-%m-%d %I:%M %p'
},
multiLineSelectMode: 'single',
labels: {
success: { single: 'Success', plural: 'Successes' },
warning: { single: 'Warning', plural: 'Warnings' },
danger: { single: 'Danger', plural: 'Dangers' },
dashboard: { title: 'Dashboard', single: 'Dashboard', plural: 'Dashboards' },
operations: { title: 'Operations', single: 'Operation', plural: 'Operations' },
locations: { title: 'Locations', single: 'Location', plural: 'Locations' },
devices: { title: 'Devices', single: 'Device', plural: 'Devices' },
customers: { title: 'Customers', single: 'Customer', plural: 'Customers' },
transactions: { title: 'Transactions', single: 'Transaction', plural: 'Transaction' },
payments: { title: 'Payments', single: 'Payment', plural: 'Payments' },
inventory: { title: 'Inventory', single: 'Inventory', plural: 'Inventories' },
iotCampaigns: { title: 'IoT Campaigns', single: 'IoT Campaign', plural: 'IoT Campaigns' },
marketing: { title: 'Marketing', single: 'Campaign', plural: 'Campaigns' },
incidents: { title: 'Incidents', single: 'Incident', plural: 'Incidents' },
reporting: { title: 'Reporting', single: 'Report', plural: 'Reports' }
}
}
);
})();
and here is where I am trying to modify it from another module/file
;(function() {
angular
.module('app.configuration')
.controller('LabelConfigCtrl', LabelConfigCtrl);
LabelConfigCtrl.$inject = ['config', 'ConfigurationService'];
function LabelConfigCtrl(config, ConfigurationService) {
var vm = this;
vm.labelModel = config.labels;
vm.updateConfig = function() {
config = vm.labelModel; <-- this does not persist if I refresh the page
}
}
})();
vm.labelModel is attached to a big form that modifies the value through ng-model

Angular data binding within components not working when using jade

I am using angular 1.6.4 and node 6.1 and I am trying to load a simple array that I have specified in the player-main component to the template but it does not get loaded correctly.
angular.
module('player').
component('playerMain', {
templateUrl: '/player.php',
controller: function PlayListController() {
this.songs = [
{
id: 'one',
title: 'Rain',
artist: 'Drake',
url: 'http://www.schillmania.com/projects/soundmanager2/demo/_mp3/rain.mp3'
},
{
id: 'two',
title: 'Walking',
artist: 'Nicki Minaj',
url: 'http://www.schillmania.com/projects/soundmanager2/demo/_mp3/walking.mp3'
},
{
id: 'three',
title: 'Barrlping with Carl (featureblend.com)',
artist: 'Akon',
url: 'http://www.freshly-ground.com/misc/music/carl-3-barlp.mp3'
},
{
id: 'four',
title: 'Angry cow sound?',
artist: 'A Cow',
url: 'http://www.freshly-ground.com/data/audio/binaural/Mak.mp3'
},
{
id: 'five',
title: 'Things that open, close and roll',
artist: 'Someone',
url: 'http://www.freshly-ground.com/data/audio/binaural/Things%20that%20open,%20close%20and%20roll.mp3'
}
];
}
});
When the template url is called 'player.php' express router redirects it to following jade template.
.container
.row
.col-md-12
h5 Songs
ul
li(ng-repeat='song in $ctrl.songs')
button(music-player='play', add-song='song') {( song.title )}
button(music-player='', add-song='song') +
button(play-all='songs') Play all
button(play-all='songs', data-play='false') Add all
hr
But even though ng-repeat does iterate over the array 5 times, the
{(song.title)}
does not get evaluated, it simply print it as a string.

Here Maps configuration with ui-leaflet

I am trying to setup Here Maps using ui-leaflet and ui-leaflet-layers.
Here is how I wrote the layer. The map container is loaded but there no map.
Can you please help with this? Thanks.
layers: {
baselayers: {
hereMapsLayer: {
name: 'Sample',
type: 'here',
layerOptions: {
id: "hereId",
code: "hereCode"
}
}
},
overlays: {}
}
Here is what worked for me:
layers: {
baselayers: {
hereMapsLayer: {
name: 'Map name',
type: 'xyz',
url: 'http://{s}.{base}.maps.cit.api.here.com/maptile/2.1/{type}/{mapID}/{scheme}/{z}/{x}/{y}/{size}/{format}?app_id={app_id}&app_code={app_code}&lg={language}',
layerOptions: {
attribution: 'Map © 2016 HERE',
subdomains: '1234',
base: 'base',
type: 'blinetile',
scheme: 'pedestrian.day',
app_id: 'app_id',
app_code: 'app_code',
mapID: 'newest',
maxZoom: 20,
language: 'eng',
format: 'png8',
size: '256'
}
}
}
}
This link from Here.com helped me resolve this problem: https://developer.here.com/news/201602172
Do not forget to remove "cit" from your url when you are ready to go live.

How do I save this angularJS to HTML5 localStorage

Feel free to tell me to go away (I'm a newbie to angular and javascript), but how would I get started on saving this angularJS to localStorage? Is it something that can be done relatively painlessly by creating a saving function in the add()? I've seen that angular has its own local-storage but can this be done without that? Pen: http://codepen.io/kiddigit/pen/LZVXMV
angular.module('app', []);
angular.module('app').controller("MainController", function(){
var vm = this;
vm.title = 'Movie Database';
vm.searchInput = '';
vm.shows = [
{
title: 'Game of Thrones',
actor: 'Dudes',
year: 2011,
favorite: true
},
{
title: 'Walking Dead',
actor: 'More dudes',
year: 2010,
favorite: false
},
{
title: 'The Goonies',
actor: 'The Coreys',
year: 2002,
favorite: true
},
{
title: 'Firefly',
actor: 'Dunno',
year: 2002,
favorite: false
},
{
title: 'Bullit',
actor: 'McQueen',
year: 2013,
favorite: true
},
];
vm.orders = [
{
id: 1,
title: 'Year Ascending',
key: 'year',
reverse: false
},
{
id: 2,
title: 'Year Descending',
key: 'year',
reverse: true
},
{
id: 3,
title: 'Title Ascending',
key: 'title',
reverse: false
},
{
id: 4,
title: 'Title Descending',
key: 'title',
reverse: true
}
];
vm.order = vm.orders[0];
vm.new = {};
vm.addShow = function() {
vm.shows.push(vm.new);
vm.new = {};
};
});
The browser (the window object) has an object called localStorage, simple as that. If you want to save an object to the local storage you better stringify it first using json.
Example:
// inside your controller:
function saveMovie() {
var selectedMovieString = json.stringify(this.selectedMovie); // for example;
localStorage.setItem("selectedMovie", selectedMovieString);
}

Create a nested view template

I am new to AngularJs, and I'm creating the nested view template like this:
http://plnkr.co/edit/u18KQc?p=preview
But the code I have is of different type. For routing, it uses this:
awfApp.config(function ($stateProvider, $urlRouterProvider) {
var states = [
{ name: 'home', isDefault: true }, //TODO: Moure a awf
{ name: 'module1', module: { title: "Module 01", img: "modul1.png" } },
{ name: 'module2', module: { title: "Module 02", img: "modul2.png" } },
{ name: 'mainlist4', module: { title: "Main List4", img: "modul2.png" } },
{ name: 'mainlist2', module: { title: "Main List2", img: "modul2.png" } },
{ name: 'mainlist', module: { title: "Main List", img: "modul2.png" } }
];
if (appConfig.doLogin)
states.push({ name: 'articles', module: { title: "Articles", img: "articles.png" } });
});
Please tell me, how can create nested views in AngularJs using the code above.
Thanks

Resources