Check whether an Object contains a specific value in any of its array

196 Views Asked by At

How to find if an object has a value?

My Object looks like below: I have to loop through the object array and check if an array has "SPOUSE" in its value. if exist set a flag spouseExits = true and store the number (in this case (4 because [SPOUSE<NUMBER>] NUMBER is 4) in a variable 'spouseIndex'

This function needs to render in IE9 as well.

eligibilityMap = {
    "CHIP": [
        "CHILD5"
    ],
    "APTC/CSR": [
        "SELF1",
        "CHILD2",
        "CHILD3",
        "SPOUSE4"
    ]
}

Code:

Object.keys(eligibilityMap).reduce(function (acc, key) {
  const array1 = eligibilityMap[key];
  //console.log('array1', array1);
  array1.forEach(element => console.log(element.indexOf('SPOUSE')))
  var spouseExist = array1.forEach(function (element) {
    //console.log('ex', element.indexOf('SPOUSE') >= 0);
    return element.indexOf('SPOUSE') >= 0;
  });
  //console.log('spouseExist', spouseExist);
  return acc;
}, {});

SpouseIndex is undefined. What am I doing wrong?

3

There are 3 best solutions below

1
Deepak On BEST ANSWER

Here's a simple approach, supports in all browsers including IE:

    var spouseExists = false;
    var spouseNumber;
    for(var key in eligibilityMap)
    {
      for(var index in eligibilityMap[key])
      {
        if (eligibilityMap[key][index].indexOf("SPOUSE") > -1)
        {
          spouseExists = true;
          spouseNumber = eligibilityMap[key][index].replace("SPOUSE", '');
          break;
        }
      }
    }
    console.log(spouseExists, spouseNumber);
1
Nguyễn Văn Phong On

Solution 1: do 3 steps

  1. You can use flatMap to get all sub-array into one.

  2. use findIndex combined with startsWith to get exactly the index of SPOUSE

  3. assign 2 variables based on the value of the found index.

const eligibilityMap = { "CHIP": [ "CHILD5" ], "APTC/CSR": ["SELF1","CHILD2","CHILD3","SPOUSE4"]};
let SpouseExits = false, SpouseIndex = 0; 
const arrays = Object.values(eligibilityMap).flatMap(r => r);
const index = arrays.findIndex(str => str.startsWith("SPOUSE"));

if(index >= 0){
  SpouseExits = true;
  SpouseIndex = arrays[index].slice(-1);
}
  
console.log({SpouseExits, SpouseIndex});

Solution 2: This function renders in IE9 as well

const eligibilityMap = { "CHIP": [ "CHILD5" ], "APTC/CSR": ["SELF1","CHILD2","CHILD3","SPOUSE4"]};
let SpouseExits = false, SpouseIndex = 0; 

for(const [key, value] of Object.entries(eligibilityMap))
{
  const index = value.findIndex(function(str){ return str.startsWith("SPOUSE")});
  if(index >= 0){
    SpouseExits = true;
    SpouseIndex = value[index].slice(-1);
  }
}

console.log({SpouseExits, SpouseIndex});

0
Ekhlas Mridha On

Your condition element.indexOf('SPOUSE') >= 0 is not matching any value spouse on your array, because your array has no value named spouse SPOUSE it has SPOUSE4 though. that's why it's returning undefined.

You may use regex instead of direct matching,

Object.keys(eligibilityMap).reduce(function (acc, key) {
const array1 = eligibilityMap[key];
//console.log('array1', array1);
// array1.forEach(element => console.log(element.indexOf('SPOUSE')))
// var spouseExist = -1;
var spouseExist = array1.filter(function (element,index) {

  if(element.match(/SPOUSE/g)) return element; //fixes
});

//fixes
console.log('spouseExist',spouseExist.length>0)
if(spouseExist.length>0){
    spouseExist.forEach(element => {
        console.log('spouseIndex',element[element.length-1])
    });
}
return acc;

}, {});

you can get the number from the spouse name directly, or you can access the index number of the matching spouse from inside the filter function using the value of index and do whatever you like. Hope this matches your requirement.