0

Say I have an array of objects that looks like this, which represents all records in a database:

let data1 = [
    {"name": "ethan", "age": 18, "class": "A", "grade": 83},
    {"name": "sam", "age": 13, "class": "A", "grade": 43},
    {"name": "mark", "age": 14, "class": "D", "grade": 33},
    {"name": "dick", "age": 14, "class": "B", "grade": 85},
    {"name": "luke", "age": 15, "class": "A", "grade": 93},
    {"name": "adam", "age": 5, "class": "C", "grade": 55},
]

I want to perform a join based on some column names, "name" and "class", since they are the primary keys.

let cols = ["name", "class"];

now I have another array of objects

let data2 = [
    {"name": "ethan", "age": 48, "class": "A", "grade": 49},
    {"name": "dick", "age": 24, "class": "B", "grade": 43},
]

I want a function that would loop through each record in data1, then check if the "name" value and "class" value match any of the records in data2 that has the same values at same columns. If yes, then the data1 record would be pushed to a new array. And the new array would be return at the end of the function.

for example, the function would start with first item in data1, {"name": "ethan", "age": 18, "class": "A", "grade": 83}, and look for a matching record in data2 that also with name "ethan" and class "A", ignoring value from other columns, if a match is found, then {"name": "ethan", "age": 18, "class": "A", "grade": 83} is pushed to the output array.

here is how I imagined what the function would look like, I would like the keys checked on to be passed in as an array so the solution is dynamic.

function getSubset(cols, data1, data2) {
    let output = [];
    for (let i = 0; i < data1; i++) { //loop through each item in data1
        let item = data1[I];
        //checks here
        //if match found in data2, push to output array
    }

    return output;
}



//expected output = 
//[ {"name": "ethan", "age": 18, "class": "A", "grade": 83},
//  {"name": "dick", "age": 14, "class": "B", "grade": 85},
//]

2
  • So, what is your specific problem? Seems to me like you got the right idea. Commented Nov 16, 2022 at 2:59
  • So loop through data2 for each row to compare, and loop trough the cols to check whether all the respective property values are the same in both items. What exactly do you have trouble with? Commented Nov 16, 2022 at 3:00

1 Answer 1

1

You could utilize various array functions to achieve it:

let data1 = [
    {"name": "ethan", "age": 18, "class": "A", "grade": 83},
    {"name": "sam", "age": 13, "class": "A", "grade": 43},
    {"name": "mark", "age": 14, "class": "D", "grade": 33},
    {"name": "dick", "age": 14, "class": "B", "grade": 85},
    {"name": "luke", "age": 15, "class": "A", "grade": 93},
    {"name": "adam", "age": 5, "class": "C", "grade": 55},
];

let data2 = [
    {"name": "ethan", "age": 48, "class": "A", "grade": 49},
    {"name": "dick", "age": 24, "class": "B", "grade": 43},
]

let cols = ["name", "class"];

function getSubset(cols, data1, data2) {
    return data1.filter(d1 => data2.some(d2 => cols.every(key => d1[key] === d2[key])));
}

const result = getSubset(cols, data1, data2);

console.log(result);

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

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.