0

I have a json array:

var arr = 
 [
  {
    "path": "a/b/c/*",
    "id": "1"
  },
  {
    "path": "l/m/*/n",
    "id": "2"
  },
  {
    "path": "a/b/c/d/*",
    "id": "3"
  }
]

I want the id of the element which matches the input param, like if I pass a input string and the array I should get the id

foo(input,arr);

so

var input = 'a/b/c/5'; //or input = 'a/b/c/4';
foo(input,arr) // should return 1

similarly

var input = 'l/m/78/n';
foo(input,arr); // should return 2

similarly

var input = 'a/b/c/d/1';
foo(input,arr); // should return 3

So I want * to be the wildcard while search. I have struggled a lot while implementing this, any help will be appreciated.

3 Answers 3

3

Convert each path into a regular expression, noting that the regular expression for a wildcard is .* instead of *.

Based on your updated question, and assuming the wildcard should match numbers only, the regular expression becomes [0-9]+:

var arr =  [{"path": "a/b/c/*","id": "1"},
            {"path": "l/m/*/n","id": "2"},
            {"path": "a/b/c/d/*","id": "3"}
           ];

function foo(input, arr) {
  var i, RE;
  
  for(i = 0 ; i < arr.length ; i++) { //iterate through the array
    RE = new RegExp(arr[i].path.replace(/\*/g, '[0-9]+')); //convert path to regexp
    if(RE.test(input)) { //test for a match
      return arr[i].id;
    }
  }
}

console.log(foo('a/b/c/5', arr));
console.log(foo('l/m/78/n', arr));
console.log(foo('a/b/c/d/1', arr));

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot, it was such a help.
I have missed one scenario and just made an update to the question, the solution will fail if I pass a/b/c/d/1, its returning id=1, but should give id=3
Can you please update the answer, I will be highly indebtful
Sure, done. My update assumes the wildcard is used only for numbers. Let me know if that's not the case.
Yeah, thats quite helpful, actually I added another check in your solution, I compare number of slashes on both sides, if equal I do RE.test(inut)
1

function foo(input,arr){

for(var i in arr){
  var re = new RegExp(arr[i].path.replace(/\*/g, '.*'));
  if (re.test(input)) {
		return arr[i].id;
  }
}

return null;

}

var arr = 
 [
  {
    "path": "a/b/c/*",
    "id": "1"
  },
  {
    "path": "l/m/*/n",
    "id": "2"
  }
];


document.write(foo("l/m/1/n",arr));

Comments

1

Here is a regex free version:

var arr = [{
	'path' : 'a/b/c/*',
	'id' : '1'
}, {
	'path' : 'l/m/*/n',
	'id' : '2'
}
]
function search(arr, input) {
input = input.split('/');
return arr.filter(function (el) {
	var value = el.path.replace('*', '').split('/');
	var passed = true;
	for (var i = 0; i < value.length; i++) {
		if (input[i].length < 1 || value[i].length < 1 || input[i] == value[i]) {}
		else {
			passed = false;
		}
	}
	return passed;
})
}
console.log(search(arr, 'a/b/c/5'));
console.log(search(arr, 'l/m/78/n'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.