Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Comparing property of object with property of another array

I am having difficulty comparing properties of two objects/arrays

First object

{
"62ac1c8d5b25ad28781a586d": {
    "selectedOption": 0
},
"62ac1c8d5b25ad28781a586e": {
    "selectedOption": 0
}}

Second Array

[
{
    "question_id": "62ac1c8d5b25ad28781a586d",
    "selected_ans": 0
},
{
    "question_id": "62ac1c8d5b25ad28781a586e",
    "selected_ans": 0
},
{
    "question_id": "62ac1c8d5b25ad28781a586f",
    "ans": 0
}

]

Kindly suggest how to compare the initial property of first object(example: 62ac1c8d5b25ad28781a586d) with the question_id property of second array and return only the matching question_id

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could simply use the find method defined on the array prototype.

function compare(id, arr){
    return arr.find(arrObj => arrObj.question_id === id);
}

To find all the matching ids from the object present in the array, you could loop through the keys and call the compare function for those keys like this.

function compare(id, arr) {
    return arr.find(arrObj => arrObj.question_id === id);
}

const obj = {
    "62ac1c8d5b25ad28781a586d": {
        "selectedOption": 0
    },
    "62ac1c8d5b25ad28781a586e": {
        "selectedOption": 0
    }
}

const arr = [
    {
        "question_id": "62ac1c8d5b25ad28781a586d",
        "selected_ans": 0
    },
    {
        "question_id": "62ac1c8d5b25ad28781a586e",
        "selected_ans": 0
    },
    {
        "question_id": "62ac1c8d5b25ad28781a586f",
        "ans": 0
    }
]
const matchingObjects = [];
for (let key in obj) {
    const matchObject = compare(key, arr);
    if (matchObject) {
        matchingObjects.push(matchObject);
    }
}
console.log(matchingObjects);

about 4 years ago · Juan Pablo Isaza Report

0

You can filter elements from secondArr using Set as:

const obj = {
    '62ac1c8d5b25ad28781a586d': {
        selectedOption: 0,
    },
    '62ac1c8d5b25ad28781a586e': {
        selectedOption: 0,
    },
};

const secondArr = [
    {
        question_id: '62ac1c8d5b25ad28781a586d',
        selected_ans: 0,
    },
    {
        question_id: '62ac1c8d5b25ad28781a586e',
        selected_ans: 0,
    },
    {
        question_id: '62ac1c8d5b25ad28781a586f',
        ans: 0,
    },
];

const keys = new Set(Object.keys(obj));
const result = secondArr.filter((o) => keys.has(o.question_id));
console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!