Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  126] [ 7]  / answers: 1 / hits: 21228  / 5 Years ago, fri, august 2, 2019, 12:00:00

I am trying to find each link value in different variables according to their name , like if name if facebook then facebooklink should filter , same lite twitter and all . But don't want to give index number . sometime fabook should be o index sometime twitter will be on 0 index . Please help



socialMediaDetails:
socialMediaDetail: Array(2)
0:
link: [email protected]
name: twitter
__typename: SocialMediaDetail
__proto__: Object
1:
link: [email protected]
name: facebook
__typename: SocialMediaDetail
__proto__: Object
2:
link: [email protected]
name: linkedIn
__typename: SocialMediaDetail

const socialMediaData=socialMediaDetails.socialMediaDetail;

const Steplabels = socialMediaData.filter((item) => {
return item.name === twitter
});


Thanks


More From » reactjs

 Answers
10

You can use find method, like this:



requiredMediaByName = (name) => {
return state.socialMediaDetails.socialMediaDetail.find(data => data.name === name);
};
console.log(requiredMediaByName('facebook'));


Note that find returns the first matching element in the array and it won't check the other elements if you get it on fist match. If you want all the elements to be returned then you should use for loop and get all of them.


[#51810] Thursday, July 25, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
josh

Total Points: 391
Total Questions: 112
Total Answers: 90

Location: Aruba
Member since Fri, Jun 24, 2022
2 Years ago
;