1

I have two array

[{ Name: 'CVS 7201 Us Highway 64',Address: '7201 Us Highway 64',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'},
{ Name: 'CVS 9931 Gilead Road', Address: '9931 Gilead Road',CustomerID: '920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{ Name: 'Walgreens',Address: '2805 N Roosevelt Blvd',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'}]

[{TenantID:1,External_ID:'920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{TenantID:2,External_ID:'FB7BCE3B-E06E-420A-A342-08CF20054F78'}]

Filtered Data

[{ Name: 'CVS 7201 Us Highway 64',Address: '7201 Us Highway 64',TenantID:2},
{ Name: 'CVS 9931 Gilead Road', Address: '9931 Gilead Road',TenantID:1},
{ Name: 'Walgreens',Address: '2805 N Roosevelt Blvd',TenantID: 2}]

In my first array there are more then 5K record and in second array it's around 100+ I write some code but i am not happy with the way.

for (const data of recordset.recordset) {//5000
    for(var i in lstAllTenant){//120
        if(lstAllTenant[i].External_ID == data.CustomerID)
        {
            var pickupLocations = {
                TenantID: lstAllTenant[i].TenantID,
                Name: data.Name,
                Address: data.Address,
            }
            lstPickupLocations.push(pickupLocations);
        }
    }
}

How can i optimize this thing?

1
  • Nothing here ? Commented Dec 6, 2018 at 13:28

4 Answers 4

3

How about this

const source = [{ Name: 'CVS 7201 Us Highway 64',Address: '7201 Us Highway 64',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'},
{ Name: 'CVS 9931 Gilead Road', Address: '9931 Gilead Road',CustomerID: '920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{ Name: 'Walgreens',Address: '2805 N Roosevelt Blvd',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'}]

const ids = [{TenantID:1,External_ID:'920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{TenantID:2,External_ID:'FB7BCE3B-E06E-420A-A342-08CF20054F78'}]

const result = source.map(i => {
i.TenantID = ids.find(id => id.External_ID === i.CustomerID).TenantID;
delete i.CustomerID;
return i
}) 

console.log(result)

Keep in mind that while this is probably simple solution, it does mutates your source (deletes CustomerID). Non-mutating version is a bit longer and is not difficult, you can achieve it on your own, if you want.

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

2 Comments

Array.prototype.map() is indeed the best function! (you where quicker!)
@PeterBode Yeah, I have the fastest hand in the West)
1

try this way:

const array1 = [{ Name: 'CVS 7201 Us Highway 64',Address: '7201 Us Highway 64',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'},
{ Name: 'CVS 9931 Gilead Road', Address: '9931 Gilead Road',CustomerID: '920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{ Name: 'Walgreens',Address: '2805 N Roosevelt Blvd',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'}];

const array2 = [{TenantID:1,External_ID:'920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{TenantID:2,External_ID:'FB7BCE3B-E06E-420A-A342-08CF20054F78'}];

const filteredArray = array1.map(({ Name, Address, CustomerID}) => ({
      Name,
      Address,
      TenantID: array2.reduce((acum, { TenantID, External_ID }) => (External_ID === CustomerID ? TenantID : acum), null)
  }));

console.log(filteredArray);

1 Comment

thanks, as per my requirement your solution is perfect.
1

What you could also do since you have a large number of items is to utilize Map and Array.map for a concise and performant solution:

const data = [{ Name: 'CVS 7201 Us Highway 64', Address: '7201 Us Highway 64', CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78' }, { Name: 'CVS 9931 Gilead Road', Address: '9931 Gilead Road', CustomerID: '920FAEF9-826B-4DE6-98F7-07E2201D5020' }, { Name: 'Walgreens', Address: '2805 N Roosevelt Blvd', CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78' } ]
const arr = [{ TenantID: 1, External_ID: '920FAEF9-826B-4DE6-98F7-07E2201D5020' }, { TenantID: 2, External_ID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78' } ]

const map = arr.reduce((r,c) => (r.set(c.External_ID, c.TenantID), r), new Map())

const result = data.map(({Name, Address, CustomerID}) => 
   ({TenantID: map.get(CustomerID), Name, Address}))

console.log(result)

Comments

0

Have a test of this - it is non destructive

const arr1 = [{ Name: 'CVS 7201 Us Highway 64',Address: '7201 Us Highway 64',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'},
{ Name: 'CVS 9931 Gilead Road', Address: '9931 Gilead Road',CustomerID: '920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{ Name: 'Walgreens',Address: '2805 N Roosevelt Blvd',CustomerID: 'FB7BCE3B-E06E-420A-A342-08CF20054F78'}]

const arr2 = [{TenantID:1,External_ID:'920FAEF9-826B-4DE6-98F7-07E2201D5020'},
{TenantID:2,External_ID:'FB7BCE3B-E06E-420A-A342-08CF20054F78'}]

let merged = arr1.map(item => {
  let item2 = arr2.find(itm2 => itm2.External_ID === item.CustomerID);
  if (item2) {
    let newItem = Object.assign({ }, item); 
    newItem.TenantID=item2.TenantID;
    delete newItem.CustomerID; // I am sure there is a way to copy only name and address using { Name, Address } = item
    return newItem;
  }  
});
console.log(merged)
console.log(arr1)

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.