Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting values from array that matches a regular expression using lodash

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"}
like image 461
Blessan Kurien Avatar asked Feb 19 '16 04:02

Blessan Kurien


People also ask

Does regex match return array?

Parameters: Here the parameter is “regExp” (i.e. regular expression) which will compare with the given string. Return Value: It will return an array that contains the matches one item for each match or if the match will not found then it will return Null.

How do I compare two arrays of objects in Lodash?

In Lodash, we can deeply compare two objects using the _. isEqual() method. This method will compare both values to determine if they are equivalent.

What is _ get?

The _. get() function is an inbuilt function in the Underscore. js library of JavaScript which is used to get the value at the path of object. If the resolved value is undefined, the defaultValue is returned in its place. Syntax: _.get(object, path, [defaultValue])

How do I get the last element of an array using Lodash?

last() method is used to get the last element of the array i.e. (n-1)th element. Parameters: This function accepts single parameter i.e. the array. Return Value: It returns the last element of the array.


1 Answers

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.

like image 102
Tushar Avatar answered Sep 28 '22 06:09

Tushar