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?

6
  • 1
    developer.mozilla.org/en-US/docs/Web/Tutorials Commented Apr 16, 2020 at 19:30
  • Does this answer your question? How to filter an array in javascript? Commented Apr 16, 2020 at 19:32
  • OP is asking for map, not filter. Commented Apr 16, 2020 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)) Commented Apr 16, 2020 at 19:34
  • @MichaelRand are you familiar with C# LINQ extension methods? This gist might be a good summary of equivalent JavaScript array methods. Commented Apr 16, 2020 at 19:35

5 Answers 5

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']
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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

Comments

0

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>

Comments

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.