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

252
Views
Compare arrays in vuejs
{
   "product_hot": {
        "default": {
            "name": "Rose",
            "values": [
                "product_rose",
                "product_default"
            ]
        },
        "exclusive": {
            "name": "Cherry Blossom",
            "values": [
                "product_cherryBlossom",
                "product_exclusive",
            ]
        }
    },
}

I have retrieved the results from the database. and it is an array :

console.log(this.productType) // result: ['product_cherryBlossom', 'product_cherryBlossom'];

How do I compare this result with the object above, to get the key as :exclusive. My question may have a lot of mistakes.Please give me more feedback..Thanks

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Assuming that below is the comparable Object

let compObj = {
   "product_hot": {
        "default": {
            "name": "Rose",
            "values": [
                "product_rose",
                "product_default"
            ]
        },
        "exclusive": {
            "name": "Cherry Blossom",
            "values": [
                "product_cherryBlossom",
                "product_exclusive",
            ]
        }
    },
}

then you can try something like

// Function that returns keys as an array

getCategoryKey(resp, compObj) { // pass the response and compObj as arguments to this function
  let category = [];
  resp.forEach(item => {
    Object.keys(compObj.product_hot).forEach(categ => {
     if(compObj.product_hot[categ].values.includes(item)) {
       category.push(categ);
     }
    }
   }
  );
  console.log('Categories corresponding to your response', category);
 return category;
}
over 4 years ago · Santiago Trujillo Report

0

Try like following snippet:

const product = {
   "product_hot": {
        "default": {
            "name": "Rose",
            "values": [
                "product_rose",
                "product_default"
            ]
        },
        "exclusive": {
            "name": "Cherry Blossom",
            "values": [
                "product_cherryBlossom",
                "product_exclusive",
            ]
        }
    },
}
const result = ['product_cherryBlossom', 'product_cherryBlossom']
const result2 = ['product_cherryBlossom', 'product_rose']

function findType(arr) {
  const res = []
  arr.forEach(r => {
    for(let key in product.product_hot) {
      if(product.product_hot[key].values.includes(r)) res.push(product.product_hot[key].name)
    }
  })
  return res
}
console.log(findType(result))
console.log(findType(result2))

over 4 years ago · Santiago Trujillo 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!