Join two arrays if field is equal in both

Asked

Viewed 115 times

-1

I have two arrays:

Exam:

[
    {
        "id": 1,
        "title": "Avaliação - Procedimento em Serras",
        "description": "Uma pequena descrição sobre a avaliação."
    },
    {
        "id": 3,
        "title": "Avaliação de Recuperação - Procedimento em Serras",
        "description": "Uma pequena descrição sobre a avaliação."
    }
]

exam_score:

[
    {
        "id": 4,
        "score": "0.00",
        "exam_id": 1
    }
]

I need to get to the field exam_id array exam_score is equal to some field id array exam. If equal, add the field score in the array merged (down below)

const merged = exams.map((e) => ({
  ...e,
  exam_score: { ...exam_score.some(({ exam_id }) => exam_id === e.id) },
}));

However, I am getting the following error:

"message": "Converting circular Structure to JSON n --> Starting at Object with constructor

Is this the right way to perform this operation? What is wrong?

  • 1

    You are using some Why? Wouldn’t it be a filter? some returns only true/false.

  • Né.. Some returns a boolean value. Thank you very much!

  • 1

    add the score field to the merged array using some Voce will only return a boolean, will not merge.

  • The code that’s in the question nay generates the error "Converting circular Structure to JSON" (see), then the problem is in another part of the code that was not shown. Please [Edit] the question by placing a [mcve] that reproduces the problem.

  • Anyway, it is not clear what the result should be. Vc says that the score should be added to the array merged, then what would that look like? Would that be https://ideone.com/5zgUrU ?

2 answers

2


The function some returns a boolean. How you want to find the value, the most indicated is to use the function find:

const exams = [
    {
        "id": 1,
        "title": "Avaliação - Procedimento em Serras",
        "description": "Uma pequena descrição sobre a avaliação."
    },
    {
        "id": 3,
        "title": "Avaliação de Recuperação - Procedimento em Serras",
        "description": "Uma pequena descrição sobre a avaliação."
    }
];

const examScore = [
    {
        "id": 3,
        "score": "10.00",
        "exam_id": 1
    }
];

const defaultScore = { score: "0.00" };

const merged = exams.map((exam) => ({
  ...exam,
  score: (examScore.find(({ id }) => exam.id === id) ?? defaultScore).score
}));

console.log(merged);


some

The method some() test whether at least one of the elements in array passes the test implemented by the assigned function and returns a value true or false.


find

The method find() returns the value of the first element of array which satisfies the test function provided. Otherwise, undefined is returned.

  • 3

    The person responsible for the downvote can explain what is wrong with the answer?

-1

He’s a boolean, so I’d settle:

exams.map(item => {
    const finded = exam_score.find(({ exam_id })=> exam_id === item.id)
    return {
        ...item,
        exam_score: finded && finded.score
    }
})
  • It’s wrong, the some returns a boolean. It needs to return the value.

  • If he needs the score I’ve edited there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.