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

108
Views
how to compare and filteredout data with next array special characters string reference in javascript

here is my question how to categorize the array data with the help of other array data elements im confusing how to compare the two array data sets with the common value

how can i access the animal# in referenceBasedValues and how to compare with referenceData elements

 const referenceData = ['animal#', 'flower#' , 'Cats#', 'dogs/colorDogs/']

    const referenceBasedValues = [{ref : 'animal#cow', value : 'Cow'},
                  {ref : 'animal#pig', value : 'Pig'},
                  {ref : 'animal#hourse', value : 'Hourse'},
                  {ref : 'flower#rose', value : 'Rose'},
                  {ref : 'flower#leafRose', value : 'Leaf Rose'},
                  {ref : 'Cats#bigcat', value : 'Big cat'},
                  {ref : 'Cats#smallcat', value : 'Small Cat'},
                  {ref : 'dogs/colorDogs/whiteDog', value : 'White dog'},
                  {ref : 'dogs/colorDogs/BlackDog', value : 'Black dog'},]

Im expecting the output like this

  const finalData = [{reference : 'animal#' , values :['Cow' , 'Pig' ,'Hourse']},
                     {reference : 'flower#' , values :['Rose' , 'Leaf Rose' ,'Hourse']},
                     {reference : 'Cats#' , values :['Big cat' , 'Small Cat']},
                     {reference : 'dogs/colorDogs/' , values :['White dog','Black dog']}
                    ]

im able to accesing animal#, flower#, Cats# with substring(0 , referenceData[i].reference.lastIndexOf("#"))

but im not able to accessing dogs/colorDogs/ word in array while filtering

If any one give answer for this one so appreciatable

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

0

// v1

const finalData = referenceData.map(reference => {
  const re = new RegExp('^' + reference);
  const values = referenceBasedValues.filter(base => re.test(base.ref)).map(base => base.value);
  return {
    reference,
    values,
  };
});

// v2

const re = new RegExp(`^(${referenceData.join('|')})`);
const finalData = Object.entries(referenceBasedValues.reduce((obj, { ref, value }) => {
  const match = re.exec(ref);
  if (match) {
    let key = match[0];
    obj[key] ??= [];
    obj[key].push(value);
  }
  return obj;
}, {})).map(([reference, values]) => ({ reference, values }));
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!