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

320
Views
How to check if there's a value (inside array) including a value that I set?

I have two datas. What I want to do is to check

(1)each contents value starts with one of categories.class_cd

(2)each contents value's length is categories.class_cd + 2

const contents = [
    "010101",
    "010101",
    "010301",
    "0103",
    "01",
    "01",
    "01400201",
    "01400201",
    "010103",
    "0118"
]

const categories = [
    {
        "class_cd": "0101",
        "name": "A",
    },
    {
        "class_cd": "0103",
        "name": "B",
    },
    {
        "class_cd": "0107",
        "name": "C",
    },
    {
        "class_cd": "0109",
        "name": "D",
    },
    {
        "class_cd": "0112",
        "name": "E",
    },
    {
        "class_cd": "0117",
        "name": "F",
    },
]

I want to get another array datas like below.

const categoriesNew =[
    {
        "class_cd": "0101",
        "name": "A",
    },
    {
        "class_cd": "0103",
        "name": "B",
    },
]

How can I sort like this??? I tried to use filter or map but it didn't work well.

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

0

If I understand your question, you can do something like this:

let res = categories.filter(category => {
    return contents.find(content => content.includes(category.class_cd) || parseInt(content) === parseInt(category.class_cd) + 2)
})

const contents = [
    "010101",
    "010101",
    "010301",
    "0103",
    "01",
    "01",
    "01400201",
    "01400201",
    "010103",
    "0118"
]

const categories = [
    {
        "class_cd": "0101",
        "name": "A",
    },
    {
        "class_cd": "0103",
        "name": "B",
    },
    {
        "class_cd": "0107",
        "name": "C",
    },
    {
        "class_cd": "0109",
        "name": "D",
    },
    {
        "class_cd": "0112",
        "name": "E",
    },
    {
        "class_cd": "0117",
        "name": "F",
    },
]

let res = categories.filter(category => {
    return contents.find(content => content.includes(category.class_cd) || parseInt(content) === parseInt(category.class_cd) + 2)
  })

console.log(res)

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!