0

I'm writing a code that has a requirement of filtering an array of objects with a string and creating a new array based on the filtered value.

Here is my code.

var a = [{
  "label": "June - 2021",
  "value": "June"
}, {
  "label": "May - 2021",
  "value": "May"
}, {
  "label": "April - 2021",
  "value": "April"
}];
var b = ["June", "May"];
var healthTemp = [];
a.forEach(item => {
  var idx = b.value.indexOf(item);
  console.log(idx);
  if (idx == 0) healthTemp.add('Previous month')
  if (idx == 1) healthTemp.add('2 months ago')
  if (idx == 2) healthTemp.add('3 months ago')
});

Here since there is June and May in b are in indexes 0, 1, I want healthTemp to be ['Previous month', '2 months ago']. But this gives me an error. Please let me know where am I going wrong and how can I fix this?

Thanks Chris for the suggestion, I've updated my question by replacing = with ==. Now I get the error as Uncaught TypeError: Cannot read property 'indexOf' of undefined".

4
  • 1
    Equality tests in Javascript use == (for coerced equality tests) or === (for uncoerced tests). = performs assignment. You are assigning idx to 0/1/2 in the if clauses, not testing its value. Commented Jul 6, 2021 at 17:49
  • There's no such thing as a "JSON Object". See also What is the difference between JSON and Object Literal Notation? Commented Jul 6, 2021 at 17:49
  • Hey @ChrisHeald, Thanks for the suggestion. I've updated my code. this gives me error as Uncaught TypeError: Cannot read property 'indexOf' of undefined" Commented Jul 6, 2021 at 18:01
  • 1
    there is no value in b. Commented Jul 6, 2021 at 18:02

3 Answers 3

1

I think what you want to do is this:

var a = [{
    "label": "June - 2021",
    "value": "June"
  }, {
    "label": "May - 2021",
    "value": "May"
  }, {
    "label": "April - 2021",
    "value": "April"
  }];
  var b = ["June", "May"];
  var healthTemp = [];
  healthTemp
  b.forEach (month => {
      a.forEach((item,idx) => {
          if(item.value == month) {
            console.log(idx);
            if (idx == 0) healthTemp.push('Previous month')
            if (idx == 1) healthTemp.push('2 months ago')
            if (idx == 2) healthTemp.push('3 months ago')
          }
      })
  })
Sign up to request clarification or add additional context in comments.

Comments

1

Another version:

const a = [{
  "label": "June - 2021",
  "value": "June"
}, {
  "label": "May - 2021",
  "value": "May"
}, {
  "label": "April - 2021",
  "value": "April"
}];

const b = ["June", "May"];



function foo(){
  const healthTemp = [];
  a.forEach(item => {
  const idx = b.indexOf(item.value);
    if(idx >=0){
      idx === 0 ? healthTemp.push('Previous month') : healthTemp.push(`${idx+1} months ago`)
    }
});
  return healthTemp
}

console.log(foo())

Comments

1

If I understand correctly your problem, I would solve it as follows:

// Given a `mont` string, find the first item of `a` whose `value` property
// equals the `month` string
// If month matches the first item of `a`, return "Previous month";
// If month matches the second item of `a`, return "2 months ago";
// If month matches the third item of `a`, return "3 months ago";
// etc...
// If month doesn't match any item of `a`, return "Never"
function howLongAgo (month) {
    for (let i=0; i<a.length; i++) {
        if (a[i].value === month) {
            return i == 0 ? "Previous month" : `${i+1} months ago`;
        }
    }
    return "Never";
}

const healthTemp = b.map(howLongAgo);

Comments

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.