1

So, I'm a SQL developer who now finds himself writing JavaScript and I have no idea what I'm doing. I need help querying objects. Let's say I have the following array of objects

    var addresses = [
    { street: '104 Bob st', city: 'Boringville', state: 'NC' },
    { street: '207 Ted rd', city: 'Quiettown', state: 'NC' },
    { street: '309 Jim ln', city: 'Lameburg', state: 'VA' }
    ];

I need to do things like get a list of states, or towns. In SQL that's a piece of cake it's just SELECT city FROM addresses, or SELECT DISTINCT state FROM addresses, or whatever you need to do. How in the world would you do this in JavaScript though?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    https://developer.mozilla.org/en-US/docs/Web/Tutorials – jonrsharpe Apr 16 '20 at 19:30
  • Does this answer your question? [How to filter an array in javascript?](https://stackoverflow.com/questions/45916135/how-to-filter-an-array-in-javascript) – Andreas Apr 16 '20 at 19:32
  • OP is asking for map, not filter. – Pedro Lima Apr 16 '20 at 19:34
  • How you do it depends on what you want to do. SELECT city FROM addresses => `addresses.map(a => a.city)`; SELECT DISTINCT state FROM addresses => `new Set(addresses.map(a => a.state))` – Heretic Monkey Apr 16 '20 at 19:34
  • @MichaelRand are you familiar with C# LINQ extension methods? [This gist](https://gist.github.com/DanDiplo/30528387da41332ff22b) might be a good summary of equivalent JavaScript array methods. – Patrick Roberts Apr 16 '20 at 19:35
  • you can load them all up into a sqlite db and use your familiar methods. – dandavis Apr 16 '20 at 19:56

5 Answers5

5

You can use the map function of arrays, which returns another array:

const result = addresses.map(address => address.state)
console.log(result) // ['NC', 'NC', 'VA']
2

You can use the .map() function. Here some exemples:

var addresses = [
    { street: '104 Bob st', city: 'Boringville', state: 'NC' },
    { street: '207 Ted rd', city: 'Quiettown', state: 'NC' },
    { street: '309 Jim ln', city: 'Lameburg', state: 'VA' }
];

var states = addresses.map(a => a.state);
var cities = addresses.map(a => a.city);
var fullAddress = address.map(a => a.street + ', ' + a.city + '/' + a.state);

You just need to pass a function as parameter. The function has to produce a new value for each element that comes from the array. In my exemples, a represents each individual element of the array. Everything that comes after the arrow (=>) is what each element should produce. You can use operations, call other functions... anything really. The result is a new array that has the same length of the original array and contains the result for each element in the same order.

Pedro Lima
  • 1,576
  • 12
  • 21
1
var addresses = [
{ street: '104 Bob st', city: 'Boringville', state: 'NC' },
{ street: '207 Ted rd', city: 'Quiettown', state: 'NC' },
{ street: '309 Jim ln', city: 'Lameburg', state: 'VA' }];


const fullAddress = addresses.map(address => {
return `State: ${address.state}, City: ${address.city}, Street:${address.street}`;
});
console.log(fullAddress);

using template string, object destructuring and implicit return, you don't need a curly bracket and return statement; A clearer way using es6

const fullAddress = addresses.map(({street, city, state}) =>
`State: ${state}, City: ${city}, Street:${street}`);
console.log(fullAddress);
Mamunur Rashid
  • 1,095
  • 17
  • 28
0

or you could use the filter method.

 var addresses = [
    { street: '104 Bob st', city: 'Boringville', state: 'NC' },
    { street: '207 Ted rd', city: 'Quiettown', state: 'NC' },
    { street: '309 Jim ln', city: 'Lameburg', state: 'VA' }
    ];
    
    const result = addresses.filter(address => address.state=='NC');
    
    console.log(result);
DCR
  • 14,737
  • 12
  • 52
  • 115
-1

You could mimic some SQL statments in Javascript, like

As an alternative, you could use a LINQ style request language, for example.

var addresses = [
        { street: '104 Bob st', city: 'Boringville', state: 'NC' },
        { street: '207 Ted rd', city: 'Quiettown', state: 'NC' },
        { street: '309 Jim ln', city: 'Lameburg', state: 'VA' }
    ],
    cities = Enumerable.From(addresses).Select('$.city').ToArray(),
    states = Enumerable.From(addresses).Select('$.state').Distinct().ToArray();
    
console.log(...cities);
console.log(...states);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392