javascript—如何将字符串拆分为单个字符,其中每个新字符作为json对象添加到数组中

x6h2sr28  于 2021-09-23  发布在  Java
关注(0)|答案(3)|浏览(201)

我有一个json对象数组,其中每个对象都有永久键“type”,其他键取决于类型。看起来是这样的:

theArray = [
    {
        "type": "text",
        "text": "= SUM("
    },
    {
        "type": "formula",
        "attrs": {
            "id": 20,
            "data": "Cost",
        }
    },
    {
        "type": "text",
        "text": ",,"
    }
]

我已经过滤了所有“type”:“text”的项目,如下所示:

this.theArray.filter(a=>a.type === 'text'))

我正在尝试拆分类型为text的每个对象,以便text键内的每个字符都是this.thearray中的一个项。
所以在这方面。上面的数组,其中“type”:“text”我想将其拆分为以下内容:

theArray = [
    {
        "type": "text",
        "text": "="
    },
    {
        "type": "text",
        "text": " "
    },
    {
        "type": "text",
        "text": "S"
    },
    {
        "type": "text",
        "text": "U"
    },
    {
        "type": "text",
        "text": "M"
    },
    {
        "type": "formula",
        "attrs": {
            "id": 20,
            "data": "Cost",
        }
    },
    {
        "type": "text",
        "text": ","
    }
    {
        "type": "text",
        "text": ","
    },
]

其中,类型为text的第一个对象按字符顺序拆分,以便将每个=、s、u、m(作为新项添加到具有“type”:“text”和text:该项的数组中。
我知道要单独拆分字符串,我需要使用.split(“”)
然而,到目前为止,我所拥有的是,它不起作用(我只得到了原始的json对象=sum(),我不确定如何进一步。有人知道如何编辑第一个数组以显示第二个数组吗?

if(theArray.filter(a=>a.type === 'text')) {
    theArray.map(a=>a.text).forEach(element => {
        element.split('')
        theArray.push({type:"text", text: element})
    })
}
o4tp2gmn

o4tp2gmn1#

你可以用 flatMap() 允许您为 text 对象,并在返回时将其展平。这里使用扩展语法(…)将字符串拆分为一个字符数组,然后再将其Map到单个对象。

const theArray = [
  {
    "type": "text",
    "text": "= SUM("
  },
  {
    "type": "formula",
    "attrs": {
      "id": 20,
      "data": "Cost",
    }
  },
  {
    "type": "text",
    "text": ",,"
  }
]

const result = theArray.flatMap(o => {
  if (o.type === 'text') {
    return [...o.text].map(char => ({ type: o.type, text: char }));
  }
  return o;
})

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
vtwuwzda

vtwuwzda2#

你可以只做一个有条件的Map el.type === 'text' :

const theArray = [
    {
        "type": "text",
        "text": "= SUM("
    },
    {
        "type": "formula",
        "attrs": {
            "id": 20,
            "data": "Cost",
        }
    },
    {
        "type": "text",
        "text": ",,"
    }
]

console.log(
    theArray
      .flatMap((el) => el.type === 'text' ? el.text.split('').map((char) => ({ type: 'text', text: char })) : el)
)
zed5wv10

zed5wv103#

你可以在这里使用平面Map
一班轮

const result = theArray.flatMap((o) =>
  o.type === "text"
    ? o.text.split("").map((s) => ({ type: o.type, text: s }))
    : o
);
const theArray = [
  {
    type: "text",
    text: "= SUM(",
  },
  {
    type: "formula",
    attrs: {
      id: 20,
      data: "Cost",
    },
  },
  {
    type: "text",
    text: ",,",
  },
];

const result = theArray.flatMap((o) => {
  if (o.type === "text") {
    return o.text.split("").map((s) => ({ type: o.type, text: s }));
  }

  return o;
});
console.log(result);

相关问题