0

I have these two arrays, one is a nested array and one is a regular array. both containing strings. I need to go through the regular array and assess if the elements match only the "claims" key and "matches" key but NOT the exclusions key. What is the best way of doing this ?

  const claimsArr = [
    {claim:'Protein',matches:['high in protein','^protein'],exclusions:['whey','milk']},
    {claim:'Carbs',matches:['high in carbs','^carbs'],exclusions:['sugar','lactose']},
     ]

  DomArray = [
        "Protein", "whey" , 
      ]
  

My desired output would be an array of matched items if the DomArray contains whats in "claim" && "matches" but not "exclusion"

expected output:

result = ["Protien", "whey", "high in protein" ,"protein"]

3
  • What does your desired output look like? Array of bools? Commented Jun 22, 2021 at 9:32
  • 1
    hi, I edited my question to state my desire output Commented Jun 22, 2021 at 9:34
  • 1
    @ShuibAbdillahi can you add the expected output for the current input as array iteslf? Commented Jun 22, 2021 at 9:42

2 Answers 2

1
const claimsArr = [
  {
    claim: "Protein",
    matches: ["high in protein", "^protein"],
    exclusions: ["whey", "milk"],
  },
  {
    claim: "Carbs",
    matches: ["high in carbs", "^carbs"],
    exclusions: ["sugar", "lactose"],
  },
];

const DomArray = ["Protein", "whey"];
const Result = [];
const data = claimsArr
  .map((x) => [x.claim, x.matches])
  .flat(2);

for (let i = 0; i < DomArray.length; i++) {
  if (data.includes(DomArray[i])) {
    Result.push(DomArray[i]);
  } else {
    continue;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Check Result for the outcome. I'm confident that less code is needed for this operation but it should do the job.
.flat().flat() could just be .flat(2)
Ah nice, thanks for the feedback, made the adjustment
1

You can use a mix of filter and includes functions of Array to achieve this.

const claimsArr = [
  {
    claim: 'Protein',
    matches: ['high in protein', '^protein'],
    exclusions: ['whey', 'milk'],
  },
  {
    claim: 'Carbs',
    matches: ['high in carbs', '^carbs'],
    exclusions: ['sugar', 'lactose'],
  },
];
const DomArray = ['Protein', 'whey'];

const isAMatch = (str) => DomArray.some(dom => str.toLowerCase().includes(dom.toLowerCase()))

const result = claimsArr.reduce((a, c) => {
  const matches = [c.claim, ...c.matches].filter(isAMatch);
  a.push(...matches);
  return a;
}, [])

console.log(result)

3 Comments

hi , this is close. i've edited my question for the expected output
Using flatMap and flat the desired output can be obtained.
put this after filter: .flatMap( (item) => [item.claim, item.matches] ).flat()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.