【问题标题】:Delete a specific string in a `react` array删除 `react` 数组中的特定字符串
【发布时间】:2022-01-06 06:09:05
【问题描述】:

您好,我正在导入value json。

console.log(value);
> Object Array(10)
 > 0
   imageUrl: "http://stackoverflow/images/0,"
 > 1
   imageUrl: "http://stackoverflow/images/1,"
 > 2
   imageUrl: "http://stackoverflow/images/2,"
 > 3
   imageUrl: "http://stackoverflow/images/3,"
 > 4 
   imageUrl: "http://stackoverflow/images/4,"
 > 5  
 
imageUrl:"http://stackoverflow/images/0http://stackoverflow/images/0,"

...
const Minsu = () => {
 
 return (
   <>
      <img src={value[0].imageUrl} />
   </>
 )

} 

我的问题是由于imageUrl中的这个,,图像没有正确输出,我只想删除这个字符串','。 但是,我尝试使用过滤器仅删除“,”字符串,但它表示无法使用过滤器,因为 imageUrl 是一个字符串。

字符串我想从特定字符串中删除',',我该怎么做?

如果imageUrl中有http://stackoverflow/images/0http://stackoverflow/images/0,我想删除一个http。

【问题讨论】:

    标签: javascript reactjs ecmascript-6 frontend


    【解决方案1】:

    使用replace() 你可以做到!

    const arr = [
      { imageUrl: "http://stackoverflow/images/0," },
      { imageUrl: "http://stackoverflow/images/1," },
      { imageUrl: "http://stackoverflow/images/2," },
      { imageUrl: "http://stackoverflow/images/3," },
      { imageUrl: "http://stackoverflow/images/4," },
      { imageUrl: "stackoverflow/images/0http://stackoverflow/images/0, " },
    ]
    function App() {
      return (
        <div>
          {arr.map(val => {
            return (
              <img src={val.imageUrl.replace(',', '').replace('stackoverflow/images/0', '')} />
            )
          })}
        </div>
      );
    }
    
    export default App;
    

    【讨论】:

    • 由于imageurlstackoverflow/images/0http://stackoverflow/images/0,我只想删除http://stackoverflow/images/0的这一部分。我该怎么办?
    • 更新我的答案!
    • 如果我的回答能帮助你接受:)
    • 哦,谢谢!但是 Array 6 7 8 .. 也得到了不同的http://stackoverflow/images/1, 3, 4 ... 输出。我想我需要剪掉这个 http
    【解决方案2】:

    试试replace

    const value = [{ imageUrl: 'http://stackoverflow/images/0,' }];
    export default function App() {
      return (
        <div>
          <h1>Hello StackBlitz!</h1>
          <p>Start editing to see some magic happen :)</p>
          return (
          <>
            <img src={value[0].imageUrl.replace(',', '')} />
          </>
          )
        </div>
      );
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多