MV Cross reference two object arrays

Hello!

Assuming I have two arrays as below. How can I, in the easiest way, without using a for loop or similar check if the player owns a company that is part of the Top companies?

Been going nuts for two hours trying to figure this out. I've been going over includes, contains, filter and forEach trying to make this go together, granted, I only slept 3 hours last night and I've been in front of the screen all day, so my brain is a wee bit mushy at this point.

But yes, this is where I am stuck at the moment... And I don't want to use a for loop as I know how to do that already, I want to learn some Array functions. And I really feel like using a combination of filter, foreach but what if there is a super easy way, perhaps using the find function would work. But please, do give me a guideline at this point 🙂

JavaScript:
    OwnedCompanies = [
        {    name: "SoftLink",
            worth: 2.1,
        },
        
        {    name: "SwindlerCo",
            worth: 1.6,
        },
    ];
    
    ExistingCompanies = [
        {    name: "SoftLink",
            worth: 2.1,
        },
        
        {    name: "Binghoo",
            worth: 1.6,
        },
        {    name: "DevCo",
            worth: 1.1,
        },
        
        {    name: "SwindlerCo",
            worth: 1.9,
        },
        {    name: "Gologolo",
            worth: 3.1,
        },
        
        {    name: "Yahoe",
            worth: 3.3,
        },
    ];
https://simplernerd.com/js-intersection/ ?

set intersection.
that's the mathematical term.
https://simplernerd.com/js-intersection/ ?

set intersection.
that's the mathematical term.
I might be wrong. But the issue that I have, or that I can see with the examples is that it's comparing first level values. I.e. arr1[2] = x.
Whereas I would want to compare arr2[4].x = y, where y is the item trying to be found.

1639778525755.png


What I want to do is this:

Remove any index from ExistingCompanies that does not also exist in Owned Companies. And the only thing of relevance in that regard would be the name, so even if the worth differs, if the company name is in both arrays, it stay in both array, if it does not exist, the index gets removed.
Only elements from the ExistingCompanies array should be removed though. So, even though JetBlack only exists as an OwnedCompany, it stays in that array.
Then I'd like to simply sort the list alphabetically.
Then I will just compare the SoftLink.worth with the other SoftLink.worth...

It sounds so easy, I think I might just go back to this in the morning zZzZzZ
Something like this?
JavaScript:
var result = ExistingCompanies.filter(function(a) {
  return OwnedCompanies.some(function(b) {
    return a.name === b.name;
  });
});
I.e. "get the ExistingCompanies entries with a name that matches at least one entry in OwnedCompanies".
If you only care about whether OwnedCompanies contains at least 1 element that's in ExistingCompanies:
JavaScript:
OwnedCompanies.some(oc => ExistingCompanies.find(ec => oc.name === ec.name));
should be enough.

If you want an Array that contains all elements from ExistingCompanies that are also in OwnedCompanies, @caethyril's solution is what you want.

You can make it even shorter using arrow functions, too:
JavaScript:
let result = ExistingCompanies.filter(a => OwnedCompanies.some(b => a.name === b.name));
If you want ALL of the object properties to match. Regardless of the name properties. In case if you need it in the future. Dump it as a string then compare. You don't need to know the property name using this way.
JavaScript:
const result = OwnedCompanies.filter(a => ExistingCompanies.some(b => JSON.stringify(a) === JSON.stringify(b)))
Thanks to all of you guys!
Something like this?
JavaScript:
var result = ExistingCompanies.filter(function(a) {
  return OwnedCompanies.some(function(b) {
    return a.name === b.name;
  });
});
I.e. "get the ExistingCompanies entries with a name that matches at least one entry in OwnedCompanies".
Thanks, this is essentially exactly what I was after. I'm naturally reworking a little, but at least I am increasing my understanding of how to use these Array functions.

If you only care about whether OwnedCompanies contains at least 1 element that's in ExistingCompanies:
JavaScript:
OwnedCompanies.some(oc => ExistingCompanies.find(ec => oc.name === ec.name));
should be enough.

If you want an Array that contains all elements from ExistingCompanies that are also in OwnedCompanies, @caethyril's solution is what you want.

You can make it even shorter using arrow functions, too:
JavaScript:
let result = ExistingCompanies.filter(a => OwnedCompanies.some(b => a.name === b.name));
I am trying to filter out any company that doesn't exist in the Existing companies array and then set those objects into another array. Then I will compare the worth value.

I have fixed this now though 🙂

EDIT:
On second thought, I haven't fixed it...
JavaScript:
    OwnedCompanies = [
        {name: "SoftLink", worth: 2.1},
        
        {name: "SwindlerCo", worth: 1.6},
        
        {name: "JetBlack", worth: 1.2}
    ];
    
    ExistingCompanies = [
        {name: "SoftLink", worth: 2.1},
        {name: "Binghoo", worth: 1.6},
        {name: "DevCo", worth: 1.1},
        {name: "SwindlerCo", worth: 1.9},
        {name: "Gologolo", worth: 3.1},
        {name: "Yahoe", worth: 3.3}
    ];
    
    MismatchedWorth = ExistingCompanies.filter(function(existingCompanies) {
          return OwnedCompanies.some(function(ownedCompanies) {
            return existingCompanies.worth != ownedCompanies.worth;
          });
        });

1639855605778.png

In this specific situation, I'd like the MismatchedWorth to contain only SwindlerCo. As SwindlerCo is a company that exist in both arrays but that has two different worth values.

But... As you can see, the MismatchedWorth contains all the companies. I've tried swapping the Company Arrays, but to no avail.

I've been fiddling around with this for some time, but sadly I'm not getting it to work 🙁 And I'll naturally keep fiddling with it, but if someone knows what's up, do tell me 🙂
EDIT:
On second thought, I haven't fixed it...
JavaScript:
MismatchedWorth = ExistingCompanies.filter(function(existingCompanies) {
          return OwnedCompanies.some(function(ownedCompanies) {
            return existingCompanies.worth != ownedCompanies.worth;
          });
        });
In this specific situation, I'd like the MismatchedWorth to contain only SwindlerCo. As SwindlerCo is a company that exist in both arrays but that has two different worth values.
Your code here only checks for a company with matching worth, not matching name AND worth.
  1. If you want to match all properties, consider using an approach like TheoAllen suggested.

  2. Alternatively you can check each property manually, e.g.
    JavaScript:
    var result = ExistingCompanies.filter(function(existingCompany) {
      return OwnedCompanies.some(function(ownedCompany) {
        return existingCompany.name  === ownedCompany.name &&
               existingCompany.worth === ownedCompany.worth;
      });
    });
Your code here only checks for a company with matching worth, not matching name AND worth.
  1. If you want to match all properties, consider using an approach like TheoAllen suggested.

  2. Alternatively you can check each property manually, e.g.
    JavaScript:
    var result = ExistingCompanies.filter(function(existingCompany) {
      return OwnedCompanies.some(function(ownedCompany) {
        return existingCompany.name  === ownedCompany.name &&
               existingCompany.worth === ownedCompany.worth;
      });
    });
Yeah, you're completely right... Don't know how I missed that fact. I did realize that I didn't filter the companies out in this scenario. But I think that is due to the fact that the companies are just a place holder... I am actually already doing the name filtering in another step. But because I filtered out a bunch of names. I didn't think about the fact that it will compare the worth of all the companies. And then, it will always find a mismatch.


JavaScript:
MismatchedWorth = ExistingCompanies.filter(existingCompanies =>
    OwnedCompanies.some(
        ownedCompanies =>
        existingCompanies.name === ownedCompanies.name &&
        existingCompanies.worth !== ownedCompanies.worth
    )
);

But yeah, this does the trick.

And again, thanks so much! 🙂

And I am still unsure what "filter" actually does at this time, it will produce a light bulb for me sooner or later. But I am quite positive that "some" takes the elements that fit the criteria(s). Is that correct?
Array#filter returns a new array with some elements removed. The removed elements are the ones that return false for the provided callback function. Details:
Array#some returns true iff at least one element of the array returns true for the provided callback function. Details:
In this case, some is being used to provide the result for filter. 👋

Original thread: https://forums.rpgmakerweb.com/threads/cross-reference-two-object-arrays.143095/