5

My json array:

[{"id":"7","name":"hello"},{"id":"7","name":"shan"},{"id":"7","name":"john"}
{"id":"7","name":"hello"}]

I want to get a new array that matches a regular expression on name starting with a letter.

I am using regexp but i don't know how to implement it.

Here is my code:

var newitem=_.filter(result,item=>item.name='hello');
 console.log(newitem);

But it returns with only strict match of name.

Please help me to modify the above so that the result is a new array as described.

Expected output

when a usertype letter h it shows only the row

{"id":"7","name":"hello"}
Timo Tijhof
  • 9,597
  • 6
  • 31
  • 45
Blessan Kurien
  • 1,649
  • 7
  • 26
  • 54

2 Answers2

13

To check if the name starts with a string, you can use RegExp#test with regex.

var newItem = _.filter(result, obj => /^[a-zA-Z]/.test(obj.name));

The regex ^[a-zA-Z] will check if the name starts with alphabet.

var arr = [{
    "id": "7",
    "name": "hello"
}, {
    "id": "7",
    "name": "shan"
}, {
    "id": "7",
    "name": "jhon"
}, {
    "id": "7",
    "name": "hello"
}, {
    id: 10,
    name: '$haun'
}];

var newItem = _.filter(arr, obj => /^[a-zA-Z]/.test(obj.name));
console.log(newItem);
<script src="https://cdnjs.com/libraries/lodash.js/"></script>

Same code can be written using Array#filter.

arr.filter(obj => /^[a-zA-Z]/.test(obj.name));

var arr = [{
    "id": "7",
    "name": "hello"
}, {
    "id": "7",
    "name": "*shan"
}, {
    "id": "7",
    "name": "jhon"
}, {
    "id": "7",
    "name": "hello"
}, {
    id: 10,
    name: '$haun'
}];

var newItem = arr.filter(obj => /^[a-zA-Z]/.test(obj.name));
console.log(newItem);
document.body.innerHTML = '<pre>' + JSON.stringify(newItem, 0, 4) + '</pre>';

Update:

when a usertype letter h it shows only the row

You can use

_.filter(result, obj => /^h/.test(obj.name));

Use i-case insensitive flag to match the alphabet irrespective of the case. That'll match both h and H.

Tushar
  • 78,625
  • 15
  • 134
  • 154
  • But it will returns all the rows – Blessan Kurien Feb 19 '16 at 05:15
  • @BlessanKurien You said _I want to get a new array that matches a regular expression on name starting with a letter._ right. What is expected output? – Tushar Feb 19 '16 at 05:18
  • @BlessanKurien Do you want to filter the objects whose name starts with `h`, then you just need to use `/^h/` regex. Use `i` flag to match case-insensitively i.e. both `h` and `H`. – Tushar Feb 19 '16 at 05:20
  • Updated my question with expected output – Blessan Kurien Feb 19 '16 at 05:26
  • I am asking regular expression in lodash library https://lodash.com/docs#isRegExp – Blessan Kurien Feb 19 '16 at 05:27
  • @BlessanKurien Check update at the bottom of the answer. And the expected output should `[{"id":"7","name":"hello"}, {"id":"7","name":"hello"}]` as their `name` starts with `h`. – Tushar Feb 19 '16 at 05:30
  • @BlessanKurien You don't need `isRegExp`, it _Checks if value is classified as a RegExp object._. You don't want to check if the value of `name` is regex. – Tushar Feb 19 '16 at 05:31
  • ok one more question instead of letter `h` i want to place variable – Blessan Kurien Feb 19 '16 at 05:33
  • @BlessanKurien You need `RegExp` constructor. Check [How do you pass a variable to a Regular Expression JavaScript?](http://stackoverflow.com/questions/494035/how-do-you-pass-a-variable-to-a-regular-expression-javascript) – Tushar Feb 19 '16 at 05:34
  • bcz when user type a keyword it is assigned to a variable and returns the rows based on that match – Blessan Kurien Feb 19 '16 at 05:34
  • I am using this way `'/^'+keyword+'/'.test(item.name)` but it gives an error `"/".test is not a function` – Blessan Kurien Feb 19 '16 at 05:37
  • @BlessanKurien Use `new RegExp("^" + keyword).test(...` – Tushar Feb 19 '16 at 05:42
  • @BlessanKurien No, you didn't mention that the keyword is dynamic. – Tushar Feb 19 '16 at 05:43
  • I've tried this `new RegExp("/^"+keyword+"/").test(item.name)` but returns null result – Blessan Kurien Feb 19 '16 at 05:45
  • @BlessanKurien READ MY LAST [COMMENT](http://stackoverflow.com/questions/35497598/getting-values-from-array-that-matches-a-regular-expression-using-loadash#comment58688101_35497671). YOU DON"T NEED `/`. – Tushar Feb 19 '16 at 05:45
0

regex ('^h.') should work on this. To get row with name h.

Rajesh Mappu
  • 456
  • 5
  • 20