3

I've 2 arrays:

address - array of object with id

pickup - array of object with id

And I need connect these arrays into 1 by id.

example:

address = [
  {id: "123", name: "any city street"},
  {id: "124", name: "any city street"},
  {id: "125", name: "any city street"},
  {id: "126", name: "any city street"}
];

pickups = [
  {id: "123", time: "10:20"},
  {id: "124", time: "11:30"},
  {id: "125", time: "12:00"},
  {id: "126", time: "12:20"}
];

And I need connect it by id with result like:

{id: "124", name: "name smth", time: "11:30"}

I mean about map this 2 arrays into one

1
  • I think you should correct expected result, for example: {id: "124", name: "any city street", time: "11:30"} Commented Jul 20, 2020 at 9:10

7 Answers 7

5

address = [
  {id: "123", name: "any city street"},
  {id: "124", name: "any city street"},
  {id: "125", name: "any city street"},
  {id: "126", name: "any city street"}
];

pickups = [
  {id: "123", time: "10:20"},
  {id: "124", time: "11:30"},
  {id: "125", time: "12:00"},
  {id: "126", time: "12:20"}
];

const result = pickups.map(pickup => {
    let match = address.find(addr => addr.id == pickup.id);
    return {
         id : pickup.id, 
         time: pickup.time,
         name : match.name,
    }
})
console.log(result);

Or if you really really like short code:

pickups.map(el => ({ ...el, ...address.find(addr => addr.id == el.id)}))

Be aware that this works fine only if there is always a match between pickups and address

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

Comments

1

You could do:

let address = [
  {id: "123", name: "any city street"},
  {id: "124", name: "any city street"},
  {id: "125", name: "any city street"},
  {id: "126", name: "any city street"}
];

let pickups = [
  {id: "123", time: "10:20"},
  {id: "124", time: "11:30"},
  {id: "125", time: "12:00"},
  {id: "126", time: "12:20"}
];

let obj = {};

for (let i=0; i<address.length; i++) {
  obj[address[i].id] = obj[address[i].id] || {id: address[i].id};
  obj[address[i].id].name = address[i].name;
}

for (let i=0; i<pickups.length; i++) {
  obj[pickups[i].id] = obj[pickups[i].id] || {id: pickups[i].id};
  obj[pickups[i].id].time = pickups[i].time;
}

let result = Object.keys(obj).map(id => obj[id]);
console.log(result);

Comments

0

const address = [
  {id: "123", name: "any city street"},
  {id: "124", name: "any city street"},
  {id: "125", name: "any city street"},
  {id: "126", name: "any city street"}
];

const pickups = [
  {id: "123", time: "10:20"},
  {id: "124", time: "11:30"},
  {id: "125", time: "12:00"},
  {id: "126", time: "12:20"}
];

const result = address.map((rec, index) => {
  return { ...rec, ...pickups[index]}
})

console.log(result)

3 Comments

this would fail if 2 arrays are in different orders
I mean that this code not matching id's, just mapping elements by index. I need to connect 2 object with the same id
I created something like this: pastebin.com/Sespzg4x But I don't know if that is in right way
0

Below snippet could help you

const addresses = [
  { id: '123', name: 'any city street' },
  { id: '126', name: 'any city street' },
  { id: '124', name: 'any city street' },
  { id: '125', name: 'any city street' },
  { id: '127', name: 'any city street' }
]

const pickups = [
  { id: '126', time: '12:20' },
  { id: '123', time: '10:20' },
  { id: '125', time: '12:00' },
  { id: '124', time: '11:30' }
]

const notUndefined = element => !!element

const res = addresses
  .map(address => {
    const pickup = pickups.find(pickup => pickup.id === address.id)
    if (pickup) {
      return { ...address, ...pickup }
    }
  })
  .filter(notUndefined)

console.log(res)

Comments

0

address = [
    { id: "123", name: "any city street" },
    { id: "124", name: "any city street" },
    { id: "125", name: "any city street" },
    { id: "126", name: "any city street" }
];

pickups = [
    { id: "123", time: "10:20" },
    { id: "124", time: "11:30" },
    { id: "125", time: "12:00" },
    { id: "126", time: "12:20" }
];

const mergedArr = address.map((item, i) => {
    if (item.id === pickups[i].id) {
        return Object.assign({}, item, pickups[i]);
    }
});

console.log(mergedArr);

2 Comments

This will be not working cuz not matching objects by IDS
Merged two arrays after checking id in the if condition.
0

Using Map

address = [ {id: "123", name: "any city street"}, {id: "124", name: "any city street"}, {id: "125", name: "any city street"}, {id: "126", name: "any city street"} ];
pickups = [ {id: "123", time: "10:20"}, {id: "124", time: "11:30"}, {id: "125", time: "12:00"}, {id: "126", time: "12:20"} ];
 map= new Map()
 address.map(o=>map.set(o.id,o))
 var res= pickups.map(o=>{
 var v=map.get(o.id); 
 if(v) v.time=o.time
  return v})
console.log(res)

Comments

0

Using Reduce:

const address = [
  {id: "123", name: "any city street"},
  {id: "124", name: "any city street"},
  {id: "125", name: "any city street"},
  {id: "126", name: "any city street"}
];

const pickups = [
  {id: "123", time: "10:20"},
  {id: "124", time: "11:30"},
  {id: "125", time: "12:00"},
  {id: "126", time: "12:20"}
];

const resultArray = [];

address.reduce((resultArr, currentAddress) => {
  let currentAddressPickup = pickups.find(pickup => pickup.id == currentAddress.id);
  let mergedCurrectAddressAndPickup = {
    ...currentAddress,
    ...currentAddressPickup
  }
  
  resultArr.push(mergedCurrectAddressAndPickup)
  return resultArr;
}, resultArray)

console.log(resultArray)

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.