2021年1月24日星期日

How to add duplicate key elements in JavaScript Object?

I want to make following newArray using by following testArray.

newArray↓

    let newArray = [          {section:"business",name:["Bob","John"]},          {section:"H&R",name:["Jen","Bobby"]},      ]  

testArray↓

let test = [      {section:"business",name:"Bob"},      {section:"business",name:"John"},      {section:"H&R",name:"Jen"},      {section:"H&R",name:"Bobby"},  ]  

First of all, I tried to find same key elements using by filter method like below.

let newArray = test.filter((x:any,i,self)=>  self.indexOf(x.section)===i  )  

but this code output is []. So, how do I make code to get my expected output? Does anyone help me?

https://stackoverflow.com/questions/65878412/how-to-add-duplicate-key-elements-in-javascript-object January 25, 2021 at 10:58AM

没有评论:

发表评论