user3872094
user3872094

Reputation: 3351

Create a new array based on an existing filtering

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

Upvotes: 0

Views: 71

Answers (3)

Marcello Del Buono
Marcello Del Buono

Reputation: 176

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);

Upvotes: 1

DoneDeal0
DoneDeal0

Reputation: 6267

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())

Upvotes: 1

λambduh
λambduh

Reputation: 93

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')
          }
      })
  })

Upvotes: 1

Related Questions