Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
934 views
in Technique[技术] by (71.8m points)

js 数组转换问题

`

let arr1= [

{role:["O","O","O","B-V","O","O","O","O","O","O","O","O","O"],
text:["Two","of","them","were","being","run","by","2","officials","of","the","Ministry","."]},

{role:["B-ARG1","I-ARG1","I-ARG1","O","O","B-V","B-ARG0","I-ARG0","I-ARG0","I-ARG0","I-ARG0","I-ARG0","O"],
text:["Two","of","them","were","being","run","by","2","officials","of","the","Ministry","."]}
]

如何转变为

let arr2= 

[{'role':'O','text':'Two'} ,{'role':'O','text':'of'},{'role':'O','text':'them'},{'role':'B-V','text':'were'},
{'role':'O','text':'being'} ,{'role':'O','text':'run'},{'role':'O','text':'by'},{'role':'O','text':'2'},
{'role':'O','text':'officials'} ,{'role':'O','text':'of'},{'role':'O','text':'the'},{'role':'O','text':'Ministry'},
{'role':'O','text':'.'}
],

[{'role':'B-ARG1','text':'Two'} ,{'role':'I-ARG1','text':'of'},{'role':'O','text':'them'},{'role':'B-V','text':'were'},
{'role':'B-V','text':'being'} ,{'role':'B-ARG0','text':'run'},{'role':'I-ARG0','text':'by'},{'role':'I-ARG0','text':'2'},
{'role':'I-ARG0','text':'officials'} ,{'role':'I-ARG0','text':'of'},{'role':'I-ARG0','text':'the'},{'role':'I-ARG0','text':'Ministry'},
{'role':'O','text':'.'}
]
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
const arr2 = arr1.map(
  m => m.role.map((role, index) => {
    return {
      role,
      text: m.text[index]
    }
  })
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...