0

I want to duplicate entire objects inside an array, based on the properties inside the object. I need to duplicate the objects, based on the split emails, in nominations.

For example

array = [
{
  id:1,
  name: ravi,
  nominations: [email protected], [email protected]
},
{
   id:2
   name: ramu,
   nominations: [email protected], [email protected]
}
]

Need Output like

Output_array = [
{
  id:1,
  name: ravi,
  nominations: [email protected]
},
{
  id:1,
  name: ravi,
  nominations: [email protected]
},
{
   id:2
   name: ramu,
   nominations: [email protected]
},
{
   id:2
   name: ramu,
   nominations: [email protected]
}
]

4 Answers 4

2

The easiest way would be to flatMap over the items, then map over the nominations.

const data = [{
  id:1,
  name: "ravi",
  nominations: "[email protected], [email protected]"
},
{
   id:2,
   name: "ramu",
   nominations: "[email protected], [email protected]"
}];

const result = data.flatMap(item => {
  return item.nominations.split(", ").map(email => ({
    id: item.id,
    name: item.name,
    nomination: email
  }))
})

console.log(result)

Sign up to request clarification or add additional context in comments.

1 Comment

@RoryMcCrossan Thanks, updated the code.
0

A pretty straightforward way using loops:

let data = [
  {
    "id": 1,
    "name": "ravi",
    "nominations": "[email protected],[email protected]"
  },
  {
    "id": 2,
    "name": "ramu",
    "nominations": "[email protected],[email protected]"
  }
];

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. If it does exist, you run a loop to individually add them.

Comments

0

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.

let input = [{
    id: 1,
    name: `ravi`,
    nominations: `[email protected], [email protected]`
},
{
    id: 2,
    name: `ramu`,
    nominations: `[email protected], [email protected]`
}];

let output = input.flatMap(i => i.nominations.split(",").map(n => Object.assign({}, i, { nominations: n.trim() })));

console.log(output);

Comments

0

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: '[email protected], [email protected]',
                      },
                      {
                         id:'2',
                         name: 'ramu',
                         nominations: '[email protected], [email protected]',
                      },
                    ];
    
    
    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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.