1

I have 2 arrays

var a = [{id: 1, name:"SFO"}, {id:2, name:"ATL"}];

var b = [{number: 1, sourceId:1, destinationId:2}, {number:2, sourceId:2, destinationId:1}];

I want to return an array in this form:

[{source: {name: "SFO"}, destination: {name: "ATL"}}, {source: {name:"ATL"}, destination: {name: "SFO"}}]

How can i use filter and reduce to produce this result above

2
  • Why reduce? map and filter should suffice Commented Dec 13, 2016 at 11:24
  • @haim770 how can i use map and filter Commented Dec 13, 2016 at 11:30

2 Answers 2

1

The code which gets expected array:

var a = [{id: 1, name:"SFO"}, {id:2, name:"ATL"}];
var b = [{number: 1, sourceId:1, destinationId:2}, {number:2, sourceId:2,  destinationId:1}];

var arr = [];
for(var i=0; i<b.length; i++){
    arr.push({
        source: {name: findName(b[i].sourceId, a) },
        destination: {name: findName(b[i].destinationId, a) }
    });
}

function findName(nr, a){
    for(var i=0; i<a.length; i++){
        if(nr==a[i].id){
            return a[i].name;
        }
    }
}

//[{source: {name: "SFO"}, destination: {name: "ATL"}}, {source: {name:"ATL"}, destination: {name: "SFO"}}]
console.log(arr);

Getting expected array using map and filter:

var a = [{id: 1, name:"SFO"}, {id:2, name:"ATL"}];
var b = [{number: 1, sourceId:1, destinationId:2}, {number:2, sourceId:2, destinationId:1}];

var arr = b.map(function(obj){
    return {
        source: { name: a.filter(function(obj2){ if(obj2.id==obj.sourceId) return obj2.name; })[0].name },
        destination: { name: a.filter(function(obj2){ if(obj2.id==obj.destinationId) return obj2.name; })[0].name }
    };
});

//[{source: {name: "SFO"}, destination: {name: "ATL"}}, {source: {name:"ATL"}, destination: {name: "SFO"}}]
console.log(arr);
Sign up to request clarification or add additional context in comments.

2 Comments

@CrogMagon can this be achieved using map and filter?
@CrogMagon i believe the time complexity would be O(n^2). Its there a way to optimize this?
0

If you have to do this you can use map() and find() but this is very inefficient and you should change your data structure.

var a = [{id: 1, name:"SFO"}, {id:2, name:"ATL"}];
var b = [{number: 1, sourceId:1, destinationId:2}, {number:2, sourceId:2, destinationId:1}];


var result = a.map(function(e) {
  var o = {}
  o.source = {
    name: e.name
  }
  var destB = b.find(function(f) {
    return e.id == f.sourceId
  })
  if (destB) {
    destB = destB.destinationId
    var destName = a.find(function(x) {
      return x.id == destB
    })
    if (destName) {
      destName = destName.name;
      o.destination = {
        name: destName
      }
    }
  }
  return o
})

console.log(result)

4 Comments

would map and filter be more efficient?
I don't think so, it would be less efficient because find() ends search when it finds first result.
so avoid map and filter?
I this case its better to use find() instead of filter()

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.