javascript - How can I find particular value from array in react native - Stack Overflow

I am trying to find each link valuein different variables according to their name , like if name if f

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

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

Share Improve this question asked Aug 2, 2019 at 4:28 Abhigyan GauravAbhigyan Gaurav 1,9047 gold badges29 silver badges63 bronze badges 2
  • So, what is the issue with the code you've posted? Please create a minimal reproducible example – adiga Commented Aug 2, 2019 at 4:30
  • by above code this code is ing while i want only name link: "[email protected]" name: "twitter" __typename: "SocialMediaDetail – Abhigyan Gaurav Commented Aug 2, 2019 at 4:33
Add a ment  | 

3 Answers 3

Reset to default 4

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.

There are a few ways this can be achieved - the simplest way would be to use a for .. of loop if you're just looking for the first item matching on name:

function findLinkByName(name) {
  for (const item of state.socialMediaDetails.socialMediaDetail) {
    if (item.name === name) {
      return item.link;
    }
  }
}

For other cases such as returning a subset of items where multiple matches on name exist, then a simple solution based on filter() and map() could be achieved like this:

function findLinksByName(name) {
  return state.socialMediaDetails.socialMediaDetail
    .filter(item => item.name === name)
    .map(item => item.link);
}

Here's a snippet showing both in action:

var state = {
  socialMediaDetails: {
    socialMediaDetail: [{
        link: "[email protected]",
        name: "twitter"
      }, {
        link: "[email protected]",
        name: "facebook"
      }, {
        link: "[email protected]",
        name: "linkedIn"
      }, {
        link: "another twitter link",
        name: "twitter"
      }
    ]
  }
}

/* 
Find link of first item matching by name (or undefined if no match found) 
*/
function findLinkByName(name) {
  for (const item of state.socialMediaDetails.socialMediaDetail) {
    if (item.name === name) {
      return item.link;
    }
  }
}

/* 
Find links for all items with matching name (or empty array if no
matches found) 
*/
function findLinksByName(name) {
  return state.socialMediaDetails.socialMediaDetail
    .filter(item => item.name === name)
    .map(item => item.link);
}

console.log(findLinkByName('twitter'));
console.log(findLinkByName('facebook'));
console.log(findLinksByName('twitter'));
console.log(findLinksByName('facebook'));

Hope that helps!

You could use:

  1. if you only want to know if the value exists or not

    var doesExist = arr.some(function(ele) { return ele.id === '21'; });

  2. If you want to get the value you are searching for:

    var data = arr.find(function(ele) { return ele.id === '21'; });

    if (data) { console.log('found'); console.log(data); // This is entire object i.e. item not boolean }

I have used following array for this example:

var arr = [];
var item1 = {
    id: 21,
    label: 'Banana',
};
var item2 = {
    id: 22,
    label: 'Apple',
};
arr.push(item1, item2);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743742534a4499411.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信