代码之家  ›  专栏  ›  技术社区  ›  user15322469

我怎样才能省略一个字符串而不有条件地呈现它呢?

  •  0
  • user15322469  · 技术社区  · 4 年前

    singlePost具有对象形式的数据。我将singlePost中的内容呈现为1到10之间的字符串。

    如何修复代码?

        const Explain = ({navigation, route}) => {
          const {singlePost} = useSelector((state) => state.post);
    
          console.log(singlePost);
    
          // singlePost = {
          //   User: {id: 3, nickname: "bill"},
          //   content1: "number1",
          //   content2: "number2",
          //   content3: "bye",
          //   content4: "empty",
          //   content5: "empty",
          //   content6: "empty",
          //   content7: "empty",
          //   content8: "number3",
          //   content9: "empty",
          //   content10: "empty",
          //   }
    
          return (
            <Ingretext>
              {singlePost?.content1}
              {'  '}
              {singlePost?.content2}
              {'  '}
              {singlePost?.content3}
              {'  '}
              {singlePost?.content4}
              {'  '}
    
              {singlePost?.content5}
              {'  '}
              {singlePost?.content6}
              {'  '}
              {singlePost?.content7}
              {'  '}
              {singlePost?.content8}
              {'  '}
              {singlePost?.content9}
              {'  '}
              {singlePost?.content10}
            </Ingretext>
          );
        };
    
        export default Explain;
    
    2 回复  |  直到 4 年前
        1
  •  1
  •   Kapobajza    4 年前

    你可以用 Object.keys 结合 Array.prototype.filter

    singlePost = {
      User: {
        id: 3,
        nickname: "bill"
      },
      content1: "number1",
      content2: "number2",
      content3: "bye",
      content4: "empty",
      content5: "empty",
      content6: "empty",
      content7: "empty",
      content8: "number3",
      content9: "empty",
      content10: "empty",
    };
    
    const contentOnly = Object
      // Get the keys of the singlePost object
      .keys(singlePost)
      // Get only keys which have the word 'content' in them
      .filter((x) => x.indexOf('content') !== -1)
      // Get data which isn't equal to 'empty' string
      .filter((key) => singlePost[key] !== "empty")
      .map((x) => singlePost[x])
      // Based on your code, I am assuming that you want the end result to be a string, so I am using .join() here
      .join(' ');
    
    console.log(contentOnly);
        2
  •  1
  •   Medi    4 年前

    reduce 要获取非空内容:

    let singlePost = {
      User: {
        id: 3,
        nickname: "bill"
      },
      content1: "number1",
      content2: "number2",
      content3: "bye",
      content4: "empty",
      content5: "empty",
      content6: "empty",
      content7: "empty",
      content8: "number3",
      content9: "empty",
      content10: "empty"
    };
    let contents = Object.keys(singlePost).reduce((acc, key) => {
      if (key.includes("content") && !singlePost[key].includes("empty")) {
        return [...acc, singlePost[key]];
      }
      return acc;
    }, []);
    console.log(contents);
        3
  •  -1
  •   filoscoder    4 年前

            <Ingretext>
              {singlePost?.content1 || "What ever when {singlePost.content1} is falsy"}
              {'  '}
              {singlePost?.content2}
              {'  '}
              {singlePost?.content3}
              {'  '}
              {singlePost?.content4}
              {'  '}
    
              {singlePost?.content5}
              {'  '}
              {singlePost?.content6}
              {'  '}
              {singlePost?.content7}
              {'  '}
              {singlePost?.content8}
              {'  '}
              {singlePost?.content9}
              {'  '}
              {singlePost?.content10}
            </Ingretext>