Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react: how to filter an array of objects in props

I have props with following data

test1: abc
id: 1
myArray: Array(3)
0: {a:abc, b:cde, aid: 1}
1: {e:age, f:ade, aid: 2}
2: {t:are, h:had, aid: 1}

I want to filter props and update array to have only the values that match id and aid

So the props should look like below:

test1: abc
id: 1
myArray: Array(3)
0: {a:abc, b:cde, aid: 1}
2: {t:are, h:had, aid: 1}

How can i do that?

like image 690
SK121212 Avatar asked Mar 23 '26 02:03

SK121212


1 Answers

You can use the filter Array method

let myArray = [{a:"abc", b:"cde", aid: 1},
               {e:"age", f:"ade", aid: 2},
               {t:"are", h:"had", aid: 1}]

const result = myArray.filter(item => item.aid === 1)

console.log(result)

But take in consideration that props are immutable.

If you wish to change this prop permanently you will have to update it in the parent component.

like image 170
caulitomaz Avatar answered Mar 25 '26 15:03

caulitomaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!