4

I have a JSON array object as follows:

var orders = [{
    orderId: 1,
    firstName: 'John',
    lastName: 'Smith',
    address: {
        street: '123 Main Street',
        city: 'New York',
        zip: 10001
    }
}, {
    orderId: 2,
    firstName: 'John',
    lastName: 'Smith',
    address: {
        street: '456 Main Street',
        city: 'New York',
        zip: 10001
    }
}, {
    orderId: 3,
    firstName: 'John',
    lastName: 'Smith',
    address: {
        street: '123 Main Street',
        city: 'New York',
        zip: 10001
    }
}, {
    orderId: 4,
    firstName: 'John',
    lastName: 'Smith',
    address: {
        street: '123 Main Street',
        city: 'New York',
        zip: 10002
    }
}];

I am trying to use underscore.js to create a new array object, grouped by the address to meet the use case of displaying all orders that have been shipped to 123 Main Street, New York, 1001.

Is underscore.js the right approach to do this? If so, how should I do so? Any hints will be helpful.

2
  • What would be the output of this? Object pointing to arrays? What would you key the objects by? Arrays of Arrays? Commented Dec 1, 2014 at 23:02
  • @JuanMendes - the output would be an array of arrays. I'm trying the suggestion you made about _.groupBy now. Commented Dec 1, 2014 at 23:14

2 Answers 2

3

See _.groupBy

console.log(_.groupBy(orders, function(obj){
    return obj.address.street + '|' + obj.address.city + '|' + obj.address.zip;
}));

See http://jsfiddle.net/mendesjuan/gc47ruyL/1/

This example assumes you cannot have a | in an address, you may need a better separator, or use JSON.stringify:

console.log(_.groupBy(orders, function(obj){
    return JSON.stringify([obj.address.street, obj.address.city, obj.address.zip]);
}));
Sign up to request clarification or add additional context in comments.

8 Comments

thanks @JuanMendes - I think groupBy will solve my problem. The JSON I posted above was an over simplified version of what I have so I am trying to put the pieces together. I will update here either way to inform if it works or not. Thanks again - everyone - for the help.
return JSON.stringify(…) always works for escaping if you don't want to choose a specific separator.
@JuanMendes: Actually I meant to stringify each string on its own, or maybe an array of them, to get a concatenation like in your answer. I didn't even think of the possibility to JSON.stringify(obj.address), which would indeed suffer from the mentioned problem.
@JuanMendes - thanks for suggesting to use _.groupBy - it was exactly what I needed. I am a new user here and am amazed by the help I was provided. I will make every effort to contribute back.
@orchard Same thing happened to me 5 years ago, I've been addicted since stackoverflow.com/questions/1868333/…
|
2

If you already have underscore in your page, you could use

var filteredOrders = _.filter(orders, function(order){
  return (order.address.street === '123 Main Street') &&
          (order.address.city === 'New York') &&
          (order.address.zip === 10001);
});

Fiddle

9 Comments

Thanks @amit_g - that is helpful, but this would work for this specific example where there is only 1 address. The JSON example I posted was representative of an array which could contain multiple users with different addresses. I need to be able to compare the address object across all users, so that I can find all users who share the same address.
@orchard, please update the question with a sample to demonstrate this requirement.
@JordanGray Actually it cannot be easily extended, it's easy if you know what you're searching for, otherwise, some heavy work is needed to find duplicates
@JuanMendes Good point; I neglected to consider excluding duplicates. Still, I feel that Amit deserves some credit for answering what was asked. :)
@JordanGray - the first thing I tried was to mark the answer as helpful :) but I am a new user and found that I don't have the required reputation (15 points) to do so ... I completely concur Amit's response is very helpful indeed.
|

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.