Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
932 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...