0

I started experimenting with functional programming lately and I'm trying to convert an old module I have written using imperative programming.

Let's say I have two arrays of objects i.e

orders: [
  {
   idOrder: 1,
   amount: 100,
   customerId: 25,
  },
  {
   idOrder: 2,
   amount: 200,
   customerId: 20,
  }
]


customers: [
  {
   customerId: 20,
   name: "John Doe",
   orders: []
  },
  {
   customerId: 25,
   name: "Mary Jane",
   orders: []
  }
]

I want to push all the orders to their respective customer. Is there a clean way of doing it?

I have tried this , but obviously it doesn't work that way :

customers.orders = orders.filter((x) => {
  if (x.customerId === customerId) {
    customer.orders.push(x);
  }
});

Thanks

3
  • 3
    please add the code you have and what you have tried. Commented Feb 7, 2017 at 15:56
  • For Java see: oracle.com/technetwork/articles/java/json-1973242.html Commented Feb 7, 2017 at 15:59
  • 1
    @donlys — But the question isn't about Java Commented Feb 7, 2017 at 16:06

4 Answers 4

2

You could use a Map and get all customers first and then push the orders to the customers.

var object = { orders: [{ idOrder: 1, amount: 100, customerId: 25 }, { idOrder: 2, amount: 200, customerId: 20 }], customers: [{ customerId: 20, name: "John Doe", orders: [] }, { customerId: 25, name: "Mary Jane", orders: [] }] },
    map = object.customers.reduce((m, a) => m.set(a.customerId, a), new Map);

object.orders.forEach(a => map.get(a.customerId).orders.push(a));

console.log(object.customers);

Sign up to request clarification or add additional context in comments.

Comments

0

Possible solution:

for (c of customers){
    c.orders.push(orders.filter( function(o){ return o.customerId === c.customerId} ));

}

1 Comment

this is going to push an array of orders into the customer.orders array. Assuming customers have no orders you could do c.orders = orders.filter(...
0

If you think of customers as your accumulator you can Reduce orders with customers as your initial value.

NOTE: this does mutate customers if you do not want this as a side-effect you would have to clone customers. Also there is not error handling for customerId not found.

var orders =  [{ idOrder: 1, amount: 100, customerId: 25 }, { idOrder: 2, amount: 200, customerId: 20}];
var customers =  [{ customerId: 20, name: "John Doe", orders: [] }, { customerId: 25, name: "Mary Jane", orders: [] } ];

var customers_orders = orders.reduce( 
               (accum, v) => 
                   { accum.find(
                            c => c.customerId == v.customerId).orders.push(v);
                     return accum; 
                   }, customers);

console.log(customers_orders);

Comments

0

You can write a function and pass it to reduce method and compose it with map

Just one things: once it's created, it may never change. You can user Object.assign and concat.

var customersWithOrders = customers.map(function(customer) {
  var relatedOrders = orders.filter(function(order) { return order.customerId === customer.customerId })
  return Object.assign(
    customer, 
    {
      orders: customer.orders.concat(relatedOrders)
    }
  )
})

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.