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

79
Views
Filter one array by type, depending other

I need to create two results from body_Task depending on the Header_Tasks.type

Header_Tasks

[
  {"_id":"AAA54" "title":"task 1" "type":1},
  {"_id":"bbb45" "title":"task 2" "type":2},
  {"_id":"ccc56" "title":"task 3" "type":1},
  {"_id":"xxx98" "title":"task 4" "type":2},
]

I have been created it depending on Header_Tasks, its name is body_taks, I save the data (header, body) in my database because it has other functions on my project

body_taks

{
  "AAA54":10,
  "AAA54|max":10,
  "bbb45":07,
  "bbb45|max":20,
  "ccc56":05,
  "ccc56|max":30,
  "xxx98":03,
  "xxx98|max":15,
}

I wish it, two results depending on Header_Tasks.type

type 1

{
  "AAA54":10,
  "AAA54|max":10,
  "ccc56":05,
  "ccc56|max":30,
}

type 2

{
  "bbb45":07,
  "bbb45|max":20,
  "xxx98":03,
  "xxx98|max":15,
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This can be done with a fairly standard 'group by' first creating a Map to look up types against, then grouping by type in the result object.

const headerTasks = [{ _id: "AAA54", title: "task 1", type: 1 }, { _id: "bbb45", title: "task 2", type: 2 }, { _id: "ccc56", title: "task 3", type: 1 }, { _id: "xxx98", title: "task 4", type: 2 },];
const bodyTasks = { AAA54: 10, "AAA54|max": 10, bbb45: "07", "bbb45|max": 20, ccc56: "05", "ccc56|max": 30, xxx98: "03", "xxx98|max": 15, };

const typeMap = new Map(headerTasks.map((t) => [t._id, t.type]));

const result = {};

for (const key in bodyTasks) {
    const id = key.includes("|") ? key.slice(0, key.indexOf("|")) : key;
    const t = typeMap.get(id);
    if (t !== undefined) {
        result[`type${t}`] ??= {};
        result[`type${t}`][key] = bodyTasks[key];
    }
}

console.log(result);

Note: I have converted the numbers prefaced with 0 to strings as [octal literals] are not allowed in strict mode and I wasn't sure if you intended them as octals.

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!