Searching data; integer to string issue

Viewed 23

I am trying to search through the data but because the IMF field is an integer I get an error when searching that field. How do I convert just that line to a string in the entire array? If I manual change every object IMF field to a string it works. When I try to JSON.stringify(jsonData) it converts everything into a different string issue and my data is undefined. It just seems to stringify the entire set of objects.

JSON.stringify

"[{'imf':6546,'description':'DEXTROSE-5%-INJ-250ML','group':'C4PV','vendor':'MEDLINE INDUSTRIES, INC. MSPV-NG','vsn':'BHL2B0062QH'},{'imf':6546,'description':'GLYCINE-1.5%-IRR-3000ML','group':'A4PV','vendor':'MEDLINE INDUSTRIES, INC. MSPV-NG','vsn':'BHL2B7317'},{'imf':6546,'description':'SET,GRAVITY,10 DROPS/ML','group':'A4PV','vendor':'MEDLINE INDUSTRIES, INC. MSPV-NG','vsn':'IME42493EH'},{'imf':465465,'description':'DEXTROSE-5%-INJ-250ML','group':'C4PV','vendor':'MEDLINE INDUSTRIES, INC. MSPV-NG','vsn':'BHL2B0062QH'},{'imf':1546,'description':'GLYCINE-1.5%-IRR-3000ML','group':'A4PV','vendor':'MEDLINE INDUSTRIES, INC. MSPV-NG','vsn':'BHL2B7317'},{'imf':2654,'description':'SET,GRAVITY,10 DROPS/ML','group':'A4PV','vendor':'MEDLINE INDUSTRIES, INC. MSPV-NG','vsn':'IME42493EH'}]"

var codesEl;
var jsonData = [
 {
            "imf": 6546,
            "description": "DEXTROSE-5%-INJ-250ML",
            "group": "C4PV",
            "vendor": "MEDLINE INDUSTRIES, INC. MSPV-NG",
            "vsn": "BHL2B0062QH"
        },
        {
            "imf": 6546,
            "description": "GLYCINE-1.5%-IRR-3000ML",
            "group": "A4PV",
            "vendor": "MEDLINE INDUSTRIES, INC. MSPV-NG",
            "vsn": "BHL2B7317"
        },
        {
            "imf": 6546,
            "description": "SET,GRAVITY,10 DROPS/ML",
            "group": "A4PV",
            "vendor": "MEDLINE INDUSTRIES, INC. MSPV-NG",
            "vsn": "IME42493EH"
        },
   {
            "imf": 465465,
            "description": "DEXTROSE-5%-INJ-250ML",
            "group": "C4PV",
            "vendor": "MEDLINE INDUSTRIES, INC. MSPV-NG",
            "vsn": "BHL2B0062QH"
        },
        {
            "imf": 1546,
            "description": "GLYCINE-1.5%-IRR-3000ML",
            "group": "A4PV",
            "vendor": "MEDLINE INDUSTRIES, INC. MSPV-NG",
            "vsn": "BHL2B7317"
        },
        {
            "imf": 2654,
            "description": "SET,GRAVITY,10 DROPS/ML",
            "group": "A4PV",
            "vendor": "MEDLINE INDUSTRIES, INC. MSPV-NG",
            "vsn": "IME42493EH"
        }
];

function printData(Arr) {
  for(var i=0; i<Arr.length; i++) {
    codesEl.innerText += `\n IMF: ${Arr[i].imf} Description: ${Arr[i].description} Group: ${Arr[i].group} Vendor: ${Arr[i].vendor} Stock: ${Arr[i].vsn}`;
    console.log(Arr)
  }
}

// it is a case insensitive search
function search(ev) {
  var key = ev.target.value;
  codesEl.innerText = null;
  
  printData(jsonData.filter((data)=>{
    var regex = new RegExp(key, "i");
    return data.imf.match(regex) || data.description.match(regex) || data.group.match(regex) || data.vendor.match(regex) || data.vsn.match(regex);
  }));
}

window.onload = function() {
  codesEl = document.getElementById("codes");
  printData(jsonData);
}
<input type="text" onkeyup="search(event)" placeholder="Enter Search Key Here" />
<div>
  <span id="codes"></span>
</div>

1 Answers

You can convert all the values to string

function search(ev) {
  var key = ev.target.value;
  codesEl.innerText = null;
  
  printData(jsonData.filter((data)=>{
    var regex = new RegExp(key, "i");
    // Here cast all the val to strings
    return Object.values(data).some(val => String(val).match(regex);
  }));
}
Related