Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

reactjs - Filter an array of objects using "includes" when mapping through array

I have an array of objects like so:

const buttonTypes = [
    {
      linkType: "default",
      icon: <LinkFilledIcon stroke={"white"} />,
    },
    {
      linkType: "twitter",
      icon: <TwitterOutlineIcon height={22} fill={"white"} />,
     
    },
    {
      linkType: "music",
      icon: <MusicOutlineIcon stroke={"white"} />,
    },
}

Now I want to use includes to filter for music when I map through the array like so:

buttonTypes.map((button, index) => {
                  console.log(button.linkType.includes(query));
                  return (
                    <div className="drop-down-option" key={index}>
                      <button
                        className="option__button"
                        onClick={(e) => {
                          props.setLinkType(button.linkType);
                          setLinkTypeDropdown(!linkTypeDropdown);
                          goToDashboard();
                        }}
                      >
                        <div className="inner__buttonContainer">
                          <div className="inner__iconContainer">
                            {button.icon}
                          </div>
                          <div className="inner__textContainer">
                            <h4>{button.title}</h4>
                          </div>
                        </div>
                      </button>{" "}
                    </div>
                  );
                })}

Any idea how I can filter the array?

question from:https://stackoverflow.com/questions/65940868/filter-an-array-of-objects-using-includes-when-mapping-through-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

first of all, you have a mistake in the closing the tag for the button array,

you can filter array like that

const buttonTypes = [
    {
      linkType: "default",
      icon: "hi",
    },
    {
      linkType: "twitter",
      icon:1,
     
    },
    {
      linkType: "music",
      icon: 2,
    },
]


buttonTypes.filter((ele) => {
    return ele.icon != 1 
}).map((ele) => {
    console.log(ele)
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...