javascript - typescript 或 JavaScript 将对象的嵌套数组转换为键、值对

标签 javascript typescript

下面的数组是输入和期望相应的 O/P。 如何使用 Typescript 将其实现为键值对

let arr = [
  {id: "1",
   questions: [
     {question: "HTML Tutorial" },
     {question: "HTML References" }
   ],
    answer : "Hypertext Markup Language is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets and scripting languages such as JavaScript and VBScript."
  },
  {id: "2",
   questions: [
     {question: "HTML Element Reference" },
     {question: "HTML Reference - Browser Support" }
   ],
   answer : "An HTML element is a type of HTML document component, one of several types of HTML nodes. HTML document is composed of a tree of simple HTML nodes, such as text nodes, and HTML elements, which add semantics and formatting to parts of document. Each element can have HTML attributes specified."
  }
];

// Expected Output using Typescript

Array [{ Key: "1", value: "HTML Tutorial, HTML References: Hypertext Markup Language is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets and scripting languages such as JavaScript and VBScript." },
{ Key: "2", value: "HTML Element Reference,HTML Reference - Browser Support : An HTML element is a type of HTML document component, one of several types of HTML nodes. HTML document is composed of a tree of simple HTML nodes, such as text nodes, and HTML elements, which add semantics and formatting to parts of document. Each element can have HTML attributes specified." }]

最佳答案

使用map()

let arr = [{id:"1",questions:[{question:"HTML Tutorial"},{question:"HTML References"}],answer:"Hypertext Markup Language is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets and scripting languages such as JavaScript and VBScript."},{id:"2",questions:[{question:"HTML Element Reference"},{question:"HTML Reference - Browser Support"}],answer:"An HTML element is a type of HTML document component, one of several types of HTML nodes. HTML document is composed of a tree of simple HTML nodes, such as text nodes, and HTML elements, which add semantics and formatting to parts of document. Each element can have HTML attributes specified."}];

let res = arr.map(i => {
  let q = i.questions.map(q => q.question).join(', ')
  return { Key: i.id, value: q + ' : ' + i.answer }
});

console.log(res)

关于javascript - typescript 或 JavaScript 将对象的嵌套数组转换为键、值对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62554333/

相关文章:

angular - 如何在 Angular 2 中从本地存储订阅项目并在更改时获取值

javascript - vuejs监听组件事件

javascript - 在动画 JavaScript 和 HTML 中制作幻灯片

javascript - 如何使其容器的输入全宽?

javascript - 读取 Angular Reactive Form 中的复选框 FormControl 值

angularjs - Angular 2 GET withCredentials(接受来自服务器的cookie)

Javascript好像没有被调用

javascript - Facebook Bot 消息延迟

node.js - 端点解析错误 - OAuth v2 Azure - msal-node

javascript - 尝试使用复选框取消选择处理从数组中删除项目