Skip to main content

How to grab a certain data from Array?

I am new to Javascript / Ionic so I try to wrap my head around different concepts.

I have a function that retrieve the users like this :

$timeout(function(){
              $scope.users = snapshot1.val();
              console.log($scope.users);
              })

Which gives me the information in the console.

Now, I would like to isolate the field "name" but when I do :

snapshot1.val().name;

It does not work. Is there a way to grab only a specific field ?

Solved

console.log(snapshot1.val().map(el=>el.name));

If its really an Array, you can generate a new Array containing just the name property using Array.prototype.map...


Comments

Popular posts from this blog

404 HEAD issue when creating AWS Elasticsearch index

I am trying to create my first index using python and I keep getting a 404 index not found exception. Here is the current code: es = Elasticsearch([{'host': 'host_url', 'port': 443, 'use_ssl': True, 'timeout': 300}]) if es.indices.exists('test_logs'): es.indices.delete(index = 'test_logs') request_body = { 'settings': { 'number_of_shards': 2, 'number_of_relicas': 2 }, 'mappings': { 'logs': { 'properties': { 'date': { 'index': 'not_analyzed', 'type': 'date' }, 'time': { 'index': 'not_analyzed', 'type': 'time' }, 'request': { 'index': 'not_analyzed', 'type': 'string' }, 'status': { 'index': 'not_analyzed', 'type': ...

What is the right way to pass a value from the UI to another thread?

I have the MainActivity thread which contains a Fragment class. The purpose of that fragment is to allow user to connect to a Bluetooth device. After the user is connected the ConnectedThread starts its while loop which is listening to incoming bytes. In the MainActivity I have a Boolean value which changes based on whether checkbox is checked or not. I would like to pass that value from the MainActivity to that while loop when it changes. So the question is, what is the right way to do that? Is there any trick like a handler for that? Or should I pass the context to the ConnectedThread and then set a listener for that in the ConnectedThread class? Solved you can use interface as callback here. declare an interface in your MainActivity and have your class with connected thread implement it. once value changes in MainActivity trigger the callback method and you can pass your variable as a parameter to other class.