首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除object值中以关键字开头的单词?javascript

如何删除object值中以关键字开头的单词?javascript
EN

Stack Overflow用户
提问于 2021-09-26 09:47:33
回答 2查看 50关注 0票数 1

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

代码语言:javascript
复制
[
  {
    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"
  }
]

我想要这样的结果。

代码语言:javascript
复制
[
  {
    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"
  }
]

我该怎么做呢?另外,我如何修剪,使末尾没有空格?

EN

回答 2

Stack Overflow用户

发布于 2021-09-26 09:51:59

1)您可以对阵列执行map操作,然后对className执行split操作,对其执行filter操作,然后执行join操作。

代码语言:javascript
复制
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);
代码语言:javascript
复制
/* 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

代码语言:javascript
复制
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);
代码语言:javascript
复制
/* 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

代码语言:javascript
复制
const result = arr.map((o) => ({
  ...o,
  className: o.className.replace(/\s*row-[^\s]+\s*/g, " ").trim(),
}));
票数 4
EN

Stack Overflow用户

发布于 2021-09-26 17:46:59

代码语言:javascript
复制
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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69333670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档