简体   繁体   English

根据属性拆分数组中的对象

[英]Split objects in array, based on the property

I want to duplicate entire objects inside an array, based on the properties inside the object.我想根据 object 中的属性复制数组中的整个对象。 I need to duplicate the objects, based on the split emails, in nominations.我需要根据拆分的电子邮件在提名中复制对象。

For example例如

array = [
{
  id:1,
  name: ravi,
  nominations: xyz@gmail.com, abc@gmail.com
},
{
   id:2
   name: ramu,
   nominations: 123@gmail.com, 456@gmail.com
}
]

Need Output like需要Output之类的

Output_array = [
{
  id:1,
  name: ravi,
  nominations: xyz@gmail.com
},
{
  id:1,
  name: ravi,
  nominations: abc@gmail.com
},
{
   id:2
   name: ramu,
   nominations: 123@gmail.com
},
{
   id:2
   name: ramu,
   nominations: 456@gmail.com
}
]

The easiest way would be to flatMap over the items, then map over the nominations.最简单的方法是对项目进行map flatMap

 const data = [{ id:1, name: "ravi", nominations: "xyz@gmail.com, abc@gmail.com" }, { id:2, name: "ramu", nominations: "123@gmail.com, 456@gmail.com" }]; const result = data.flatMap(item => { return item.nominations.split(", ").map(email => ({ id: item.id, name: item.name, nomination: email })) }) console.log(result)

A pretty straightforward way using loops:使用循环的一种非常简单的方法:

 let data = [ { "id": 1, "name": "ravi", "nominations": "xyz@gmail.com,abc@gmail.com" }, { "id": 2, "name": "ramu", "nominations": "123@gmail.com,456@gmail.com" } ]; let newData = [] for (let element of data) { let emailIds = element.nominations.split(","); if (emailIds.length > 1) { for (let emailId of emailIds) newData.push({ id: element.id, name: element.name, nominations: emailId }) } } console.log(newData)

Explanation : Starting with traversing the entire objects, in each object you split the nominations string and split it with "," to check if there is more than one email.解释: 从遍历整个对象开始,在每个 object 中拆分nominations字符串并用“,”拆分,以检查是否存在多个 email。 If it does exist, you run a loop to individually add them.如果确实存在,则运行循环以单独添加它们。

This could be done with an arguably long one-liner.这可以用一个可以说是长的单线来完成。 Object.assign function can be employed to duplicate input object while changing property/properties of interest. Object.assign function 可用于在更改感兴趣的属性/属性时复制输入 object。

 let input = [{ id: 1, name: `ravi`, nominations: `xyz@gmail.com, abc@gmail.com` }, { id: 2, name: `ramu`, nominations: `123@gmail.com, 456@gmail.com` }]; let output = input.flatMap(i => i.nominations.split(",").map(n => Object.assign({}, i, { nominations: n.trim() }))); console.log(output);

You can try this code:你可以试试这段代码:

 <:DOCTYPE html> <html> <head> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document):ready(function(){ var data_array = [ { id,'1': name, 'ravi': nominations. 'xyz@gmail,com. abc@gmail,com', }: { id,'2': name, 'ramu': nominations. '123@gmail,com. 456@gmail,com', }; ]. var output_array = data_array.flatMap(key => { return key.nominations,split("."):map(split_email => ({ id. key,id: name. key,name: nomination. $;trim(split_email) })) }). console;log(output_array); }); </script> </head> <body> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM