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

114
Views
Javascript json filter with arrow function

I was wondering how I can filter the values "tags" in the json above using an arrow function:

const ttag = [{
    "code": 795302828,
    "code_integration": "123",
    "company": "ACME LTD",
    "phone": "135575788",
    "tags": [{"tag": "companyAA"},
             {"tag": "companyBB"},
             {"tag": "companyCC"},
             {"tag": "companyDD"}
            ],
    "status": "Y"
}]

const onlyTags = ttag.filter(f => f.tags)
console.log(onlyTags)

expected result: ['companyAA', 'companyBB', 'companyCC', 'companyDD']

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

0

First of all, that's not how filter method works. Filter returns an array with values that are true for function passed as the argument. More you can read here.

You could use map & flatMap, like this:

ttag.flatMap(f => f.tags.map(t => t.tag));

map returns array of values specified in the passed method. flatMap does the same, but if the result is an array it flattens it, so result is an array of strings instead of an array of arrays of strings.

Important

flatMap is not supported in Internet Explorer, but it's already unsupported browser so I wouldn't bother.

about 4 years ago · Juan Pablo Isaza Report

0

Maybe an ugly way to do it, but with the first .map you get each item of the main array, and with the second .map an array of tags inside it.

const ttag = [{
    "code": 795302828,
    "code_integration": "123",
    "company": "ACME LTD",
    "phone": "135575788",
    "tags": [{"tag": "companyAA"},
             {"tag": "companyBB"},
             {"tag": "companyCC"},
             {"tag": "companyDD"}
            ],
    "status": "Y"
}]

const onlyTags = ttag.map(f => f.tags.map(t => t.tag));
console.log(onlyTags);
console.log(onlyTags[0]); // expected output

about 4 years ago · Juan Pablo Isaza Report

0

Here's one solution using map and reduce. At the end the array is flatten so it returns only an array containing the city names

const ttag = [{
  "code": 795302828,
  "code_integration": "123",
  "company": "ACME LTD",
  "phone": "135575788",
  "tags": [{
      "tag": "companyAA"
    },
    {
      "tag": "companyBB"
    },
    {
      "tag": "companyCC"
    },
    {
      "tag": "companyDD"
    }
  ],
  "status": "Y"
}]

const result = ttag.map(obj => obj.tags.reduce((acc, next) => [...acc, next.tag], [])).flat()
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!