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".

user3872094
  • 3,269
  • 8
  • 33
  • 71
  • 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. – Chris Heald Jul 06 '21 at 17:49
  • [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). See also [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/q/2904131) – VLAZ Jul 06 '21 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"` – user3872094 Jul 06 '21 at 18:01
  • 1
    there is no value in b. – DoneDeal0 Jul 06 '21 at 18:02

3 Answers3

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')
          }
      })
  })
λambduh
  • 85
  • 8
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())
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
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);