0

I need to search the value of each label , for any number match the user input if user input same as any number of the value , it shows the label , the code below is for showing result if input match the range between two numbers which I don't want

var testArray = [{
  "label": "Nordea",
  "value": ["77038-3532 - 19428 - 18073 - 08028 - 07058 - 07936 - 77038 - 3532 "]
}, {
  "label": "Swedbank",
  "value": ["77038-3532 - 19428 - 18073 - 08028 - 07058 - 07936 - 77038 - 3532 "]
}, {
  "label": "Sparbanken Nord",
  "value": "8264"
}, {
  "label": "Sparbanken i Enköping",
  "value": ["7121-7122", "8305-5"]
}];

function findTestObject(input) {
  return testArray.find(function (testObj) {
    var value = testObj.value;

    if (Array.isArray(value)) {
      return value.some(function (range) {
        var rangeData = range.split('-');
        return parseInt(rangeData[0]) <= input && input <= parseInt(rangeData[1]);
      });
    } else {
      return value === input;
    }
  });
}

function test() {
  var userInput = document.getElementById("input").value;
  var result = findTestObject(userInput);
  var label = result ? result.label : 'Not Found';
  document.getElementById("result").innerHTML = label;
}
<input id="input" placeholder="Enter something..."/>
<button onclick="test()">Test</button>
<br />
Result: <div id="result"></div>
J.Ali
  • 5
  • 1
  • 6
  • I got the code above from this https://stackoverflow.com/questions/40140929/check-if-input-is-between-two-values-multiple-conditions – J.Ali Sep 04 '18 at 14:46
  • So user will enter this complete string `77038-3532 - 19428 - 18073 - 08028 - 07058 - 07936 - 77038 - 3532` in input? – brk Sep 04 '18 at 14:57
  • no , only one number , for example 07058 or 19428 – J.Ali Sep 04 '18 at 14:58
  • First question: what have you tried yourself? That's an important part of questions on StackOverflow. – Scott Sauyet Sep 04 '18 at 15:20
  • Second one: how are your `values` supposed to be organized? I can make little sense out of the examples. Some things look like ranges, others like single values, and still others like parts numbers including a hyphen. One is not even an array, but a single string. What format(s) can we expect? What do they mean? Are there ranges, for instance. – Scott Sauyet Sep 04 '18 at 15:24
  • @ScottSauyet I tried different things , couldn't solve the issue , I am no developer is just a hobby , it is simple search , for example you put the zip code of a city it shows you which nearest sea port or airport ... – J.Ali Sep 04 '18 at 15:51

2 Answers2

0

If I understood your question correctly, this should be what you are looking for:

var testArray = [{
  "label": "Nordea",
  "value": ["77038-3532 - 19428 - 18073 - 08028 - 07058 - 07936 - 77038 - 3532 "]
}, {
  "label": "Swedbank",
  "value": ["77038-3532 - 19428 - 18073 - 08028 - 07058 - 07936 - 77038 - 3532 "]
}, {
  "label": "Sparbanken Nord",
  "value": "8264"
}, {
  "label": "Sparbanken i Enköping",
  "value": ["7121-7122", "8305-5"]
}];

function findTestObject(input) {
  return testArray.find(function (testObj) {
    var value = testObj.value;

    if (Array.isArray(value)) {
      return value.some(function (range) {
        return parseInt(range === input);
      });
    } else {
      return value === input;
    }
  });
}

function test() {
  var userInput = document.getElementById("input").value;
  var result = findTestObject(userInput);
  var label = result ? result.label : 'Not Found';
  document.getElementById("result").innerHTML = label;
}
<input id="input" placeholder="Enter something..."/>
<button onclick="test()">Test</button>
<br />
Result: <div id="result"></div>
DaniFoldi
  • 451
  • 4
  • 14
  • try search for any of these numbers 19428 - 18073 - 08028 - 07058 - 07936 - 77038 - 3532 ) in Nordea label , it shows not found – J.Ali Sep 04 '18 at 14:55
  • @J.Ali in that case I didn't fully understand your question. You want the text "Nordea" to appear when one of those numbers is entered? – DaniFoldi Sep 04 '18 at 15:04
  • Also please note that your data model isn't consistent, in the case of Sparbanken Nord, the value is of type string, in other cases it is array. – DaniFoldi Sep 04 '18 at 15:10
  • yes even after removing Sparbanken , still same issue – J.Ali Sep 04 '18 at 15:15
0

This is using match, because I think it's easier to deal with different types of data stored in value.

function findTestObject(input) {

var testArray = [{
    "label": "Nordea",
    "value": ["77038-3532 - 19428 - 18073 - 08028 - 07058 - 07936 - 77038 - 3532 "]
  },
     {
    "label": "Sparbanken Nord",
    "value": "8264"
  }, {
    "label": "Sparbanken i Enköping",
    "value": ["7121-7122", "8305-5"]
  }];

  var r = new RegExp(input, "gi");

  var result = [];

  testArray.forEach(e=>{
    if (e.value.toString().match(r)) {
        result.push(e.label);
      }
  })

  return result;
}

function test() {
  var userInput = document.getElementById("input").value;
  var result = findTestObject(userInput);
  document.getElementById("result").innerHTML = (result.join(" ") || "Not Found");
}
<input id="input" placeholder="Enter something..." />
<button onclick="test()">Test</button>
<br /> Result:
<div id="result"></div>

I'm not considering if user put an -, that match several values, but it's good as example, you could exclude it easily.

Emeeus
  • 5,072
  • 2
  • 25
  • 37