javascript - 将字符串转换为对象数组的最佳方法是什么?有具体图案吗?

标签 javascript lodash

我有一个字符串数组,其中使用了一些 HTML 代码。例如

["An ultrasound-guided biopsy was performed for the <code>[1.4 cm solid]</code> mass located in the [right ]breast, at the [12:00] position.  This was described on the previous[ Ultrasound and mammography reports.]  The skin was prepped in the usual manner.  Local anesthetic was administered to the access site.  [A guiding needle was inserted initially.]  Subsequently, a [14] gauge biopsy needle was placed adjacent to the abnormality under ultrasound guidance.  Once the needle was documented to be in the correct location, [4 ]core specimens were obtained using a [Bard automated firing device.]  A metallic clip was inserted into the breast at the location of the biopsy.  A bandage was applied to the access site.  Post procedure mammographic imaging was done.  The specimens were sent to the laboratory for pathological analysis.&nbsp;&nbsp;","\n","Complications: <code>[none]</code>","\n<p></p>\n"]

我想将字符串拆分为一些模式并希望得到这个结果

 [{
    "text": "An ultrasound-guided biopsy was performed for the"
}, {
    "text": "1.4 cm solid"
    "value":"code"  
},{
  "text":"mass located in the [right ]breast, at the [12:00] posit… to the laboratory for pathological analysis.&nbsp;&nbsp"
}]

最佳答案

您可能会发现 the DOMParser有用。

const arr = ["An ultrasound-guided biopsy was performed for the <code>[1.4 cm solid]</code> mass located in the [right ]breast, at the [12:00] position.  This was described on the previous[ Ultrasound and mammography reports.]  The skin was prepped in the usual manner.  Local anesthetic was administered to the access site.  [A guiding needle was inserted initially.]  Subsequently, a [14] gauge biopsy needle was placed adjacent to the abnormality under ultrasound guidance.  Once the needle was documented to be in the correct location, [4 ]core specimens were obtained using a [Bard automated firing device.]  A metallic clip was inserted into the breast at the location of the biopsy.  A bandage was applied to the access site.  Post procedure mammographic imaging was done.  The specimens were sent to the laboratory for pathological analysis.&nbsp;&nbsp;","\n","Complications: <code>[none]</code>","\n<p></p>\n"];

var domparser = new DOMParser();

// Parse the string in the array to a new HTML document
const parsed = domparser.parseFromString(arr[0], 'text/html');

// Iterate over the child nodes with reduce to produce a new array of objects
const obj = [...parsed.body.childNodes].reduce((acc, node) => {

  // Destructure the relevant props from each node
  const { nodeType, textContent, tagName } = node;

  // Trim the edge spaces from the text
  const text = textContent.trim();

  // If the nodeType is 3 (text node) return an object with
  // just the text
  if (nodeType === 3) return acc.concat({ text });

  // Otherwise get the value
  const value = tagName.toLowerCase();

  // Do further string replacement
  const replacedText = text.replace(/[\[\]]/g, '');

  // Return the object with the text and the value
  return acc.concat({ text: replacedText, value });
}, []);

console.log(obj);

进一步阅读

关于javascript - 将字符串转换为对象数组的最佳方法是什么?有具体图案吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57691029/

相关文章:

javascript - javascript中计算不正确的问题

javascript - 获取提到的用户的状态/事件

javascript - 展平包含对象的数组的数组

java - 带有 Apache Tiles 的 AngularJS

javascript - 当每个函数返回 deferred.promise 时,如何链接函数数组的执行?

javascript - 使用 Lodash/Javascript 过滤嵌套数组

javascript - lodash 使用 _.get() 进行深度观察

javascript - lodash:从对象数组中获取对象值

javascript - 使用 Lodash 和 Moment 从单个日期按天/小时分组数组

javascript - IE9 中未定义初始传递的 For 循环索引