【问题标题】:How do I delete words that start with the keyword in the object value? javascript如何删除以对象值中的关键字开头的单词? javascript
【发布时间】:2021-09-26 09:47:33
【问题描述】:

我有一个这样的数组。我想删除className 属性中的row-? 字词。

[
  {
    type: "text",
    name: "text-1632646960432-0",
    satir: "1",
    className: "form-control col-lg-3 row-1"
  },
  {
    type: "text",
    name: "text-1632646974512-0",
    satir: "1",
    className: "form-control col-lg-6 row-8"
  }
]

我想要这样的结果。

[
  {
    type: "text",
    name: "text-1632646960432-0",
    satir: "1",
    className: "form-control col-lg-3"
  },
  {
    type: "text",
    name: "text-1632646974512-0",
    satir: "1",
    className: "form-control col-lg-6"
  }
]

我该怎么做?另外我怎样才能修剪到最后没有空间?

【问题讨论】:

    标签: javascript arrays json object


    【解决方案1】:

    1)你可以map覆盖数组,然后splitclassNamefilter它,然后join它。

    const arr = [
      {
        type: "text",
        name: "text-1632646960432-0",
        satir: "1",
        className: "form-control col-lg-3 row-1",
      },
      {
        type: "text",
        name: "text-1632646974512-0",
        satir: "1",
        className: "form-control col-lg-6 row-8",
      },
    ];
    
    const result = arr.map((o) => ({
      ...o,
      className: o.className
        .split(" ")
        .filter((s) => !s.startsWith("row"))
        .join(" "),
    }));
    
    console.log(result);
    /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    2)你也可以使用正则表达式/\s*row-\d+/g

    const arr = [{
        type: "text",
        name: "text-1632646960432-0",
        satir: "1",
        className: "form-control col-lg-3 row-1",
      },
      {
        type: "text",
        name: "text-1632646974512-0",
        satir: "1",
        className: "row-12 form-control row-6 col-lg-6 row-8",
      },
    ];
    
    const result = arr.map((o) => ({
      ...o,
      className: o.className.replace(/\s*row-\d+/g, "").trim(),
    }));
    
    console.log(result);
    /* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    /\s*row-[^\s]+\s*/g

    const result = arr.map((o) => ({
      ...o,
      className: o.className.replace(/\s*row-[^\s]+\s*/g, " ").trim(),
    }));
    

    【讨论】:

      【解决方案2】:

      const data =[
        {
          type: "text",
          name: "text-1632646960432-0",
          satir: "1",
          className: "form-control col-lg-3 row-1"
        },
        {
          type: "text",
          name: "text-1632646974512-0",
          satir: "1",
          className: "form-control col-lg-6 row-8"
        }
      ];
      
      const results = data.map((item) => {
        if (item.className.includes('row')) {
          return {
            ...item,
            className: item.className.replace(/row-[0-9]/, '').trim()
          }
        }
      });
      
      console.log(results);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-05
        • 2012-09-30
        • 2021-02-12
        • 2016-02-08
        • 2018-12-22
        • 1970-01-01
        • 2012-01-31
        相关资源
        最近更新 更多