Reputation: 2685
Him I am new to the react redux
. I have an array of objects which is like,
let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'}{Id :'a',temp:5,data:'xyz (c:1'}]
Now, what I want to do is that there is key called data
with some value, I want to take the value which is before that bracket only in that data key.
what I want to look an object ,
{Id :'a',count:5,data:'abc'}
what I have is , I have an regex which I use to get that value,
and I have used for loop ,
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^[^\(]+/;
let value = data.match(regex)[0].trim();
result[i].data = value
}
fetchQuestions() {
let previous_data = { ...this.props.data };
let result = [];
for (let key in previous_data) {
result = [...result, ...previous_data[key]];
}
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^(.*)\(/;
let value = result[i].technology.match(regex)[1].trim();
result[i].technology = value;
}
this.props.fetchQuestions(result);
}
In this this.props.data
is the reducer data .Now what I want to do was, I have to get some data from this and do some manipulation. but here, it is directly updating my reducer values itself, But I have used spread operator to get the copy of that data ,but still its updating the reducers values.
But I am not getting the value which I expect. Can any one help me with this ?
Upvotes: 0
Views: 95
Reputation: 281656
You can write a regex that creates a group for the value before (
and then get it from the match
let result= [
{
Id: 'a',
temp: 5,
data: 'abc (c:1'
},
{
Id: 'a',
temp: 5,
data: 'pqr (c:1'
},
{
Id: 'a',
temp: 5,
data: 'xyz (c:1'
}
]
for (let i = 0; i <= result.length - 1; i++) {
let regex = /^(.*)\(/;
let value = result[i].data.match(regex)[1].trim();
result[i].data = value
}
console.log(result);
Upvotes: 1
Reputation: 9
let result = [ {Id :'a',temp:5,data:'abc (c:1'},{Id :'a',temp:5,data:'pqr (c:1'},{Id :'a',temp:5,data:'xyz (c:1'}]
const new_result = result.map((item) => {
let regex = /^[^\(]+/;
let value = item.data.match(regex)[0].trim();
item.data = value;
return item;
});
console.log(new_result);
Upvotes: 0
Reputation: 292
Try this one.
result.map(el => {
el.data = el.data.match(/^(\w+)/)[0];
return el;
})
Upvotes: 0
Reputation: 370689
You need to access the data
property of each object while you're iterating, in order to use a .match
on it:
let result = [{
Id: 'a',
temp: 5,
data: 'abc (c:1'
}, {
Id: 'a',
temp: 5,
data: 'pqr (c:1'
}, {
Id: 'a',
temp: 5,
data: 'xyz (c:1'
}];
for (let i = 0; i <= result.length - 1; i++) {
const { data } = result[i];
let regex = /^[^\(]+/;
let value = data.match(regex)[0].trim();
result[i].data = value;
}
console.log(result);
But, seeing as you're using react, you might consider using .map
instead, which won't mutate the original objects:
let result = [{
Id: 'a',
temp: 5,
data: 'abc (c:1'
}, {
Id: 'a',
temp: 5,
data: 'pqr (c:1'
}, {
Id: 'a',
temp: 5,
data: 'xyz (c:1'
}];
console.log(
result.map(({ data, ...rest }) => ({
data: data.match(/^[^(]+[^( ]/)[0],
...rest
}))
);
Note that if you make the final matched character in the regex [^( ]
(match anything but a (
or a space), there's no need for trim
anymore.
Upvotes: 1
Reputation: 30739
You can use split('(')
instead of regex to get the same output:
let result = [{
Id: 'a',
temp: 5,
data: 'abc (c:1'
}, {
Id: 'a',
temp: 5,
data: 'pqr (c:1'
}, {
Id: 'a',
temp: 5,
data: 'xyz (c:1'
}];
for (let i = 0; i <= result.length - 1; i++) {
let value = result[i].data.split('(')[0].trim();
result[i].data = value
}
console.log(result);
Upvotes: 0