I have a set of objects structured like this:
const obj_arr = [
{
id: '1',
jobs: [
{
completed: false,
id: '11',
run: {
id: '6',
type: 'type1',
},
},
{
completed: true,
id: '14',
run: {
id: '17',
type: 'type1',
},
},
{
completed: false,
id: '12',
run: {
id: '7',
type: 'type2',
},
},
],
},
{
id: '2',
jobs: [
{
completed: true,
id: '13',
run: {
id: '8',
type: 'type2',
},
},
{
completed: true,
id: '16',
run: {
id: '9',
type: 'type1',
},
},
{
completed: true,
id: '61',
run: {
id: '19',
type: 'type1',
},
},
],
},
{
id: '3',
jobs: [
{
completed: false,
id: '111',
run: {
id: '62',
type: 'type1',
},
},
],
},
],
and an arr_ids = ["1","2"]
Now, I need to extract and filter out the ids from obj_arr based on arr_ids, which can be done as follows:
const filteredIds = obj_arr.filter(obj => arr_ids.includes(obj.id));
So the filtered_objarr = [
{
id: '1',
jobs: [
{
completed: false,
id: '11',
run: {
id: '6',
type: 'type1',
},
},
{
completed: true,
id: '14',
run: {
id: '17',
type: 'type1',
},
},
{
completed: false,
id: '12',
run: {
id: '7',
type: 'type2',
},
},
],
},
{
id: '2',
jobs: [
{
completed: true,
id: '13',
run: {
id: '8',
type: 'type2',
},
},
{
completed: true,
id: '16',
run: {
id: '9',
type: 'type1',
},
},
{
completed: true,
id: '61',
run: {
id: '19',
type: 'type1',
},
},
],
},
]
Next step is to identify the ids from filtered_objarr where runs are of type "type1" and none of the jobs are marked as completed: false.
Hence, the expected output from filtered_objarr should be "2".
In the given example, the id "1" is omitted because it contains jobs with both completed: true and completed: false for type "type1".
What I've attempted so far?
const output = filtered_objarr.map(obj => obj.jobs.map(job => job.run.type ===
"type1" && job.completed === true));
Upon logging the output, it returns:
[
[false,true,false],
[true,true,true]
]
This approach doesn't provide me with the id of the object that has a job run of type "type1" with no incomplete jobs. How can I achieve that? Any assistance would be greatly appreciated as I am new to programming and still learning. Thank you.