Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
How to create two objects from one object if fields are not same in object?

I have below array of objects and I have to create a new array of objects based on the below conditions

  1. if id is not empty and registration id is empty push this object into the result array.

  2. if registration id is not empty and id is empty then assign registration id to id and push this object into the result array.

  3. if id is not empty and registration id is not empty then

    a.check id and registration id are the same then assign registration id to id push this object into the result array.

    b. id and registration id are not same create two object .one with "id": 1 and another one with assign registration id to id("id": 100)

var userData =[{
"id": 1,
"name": "abcd",
"place":"xyz",
"registrationid": 100


}];

var result = [];

userData.forEach( (element) => {
   if(element.id !="" && element.registrationid ==""){
        console.log("1");
        result.push(element);
        
   } else if(element.registrationid !="" && element.id ==""){
        console.log("2");
        element.id  = element.registrationid;
        result.push(element);
        
   } else if(element.registrationid !="" && element.id !=""){
             console.log("3");
          if(element.id  == element.registrationid){
          console.log("4");
            element.id  = element.registrationid;
              result.push(element);
            
        }else if(element.id  != element.registrationid){
          console.log("5");
          result.push(element)
          element.id  = element.registrationid;
            result.push(element)
        }
        
   }
});

console.log(result)

Expected output result array should return

[
{
"id": 1,
"name": "abcd",
"place":"xyz",
"registrationid": 100
},
{
"id": 100,
"name": "abcd",
"place":"xyz",
"registrationid": 100
},

];

Gone through this but could not get any idea please help me out guys

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Create a copy in step 3b:

var userData = [{
  "id": 1,
  "name": "abcd",
  "place": "xyz",
  "registrationid": 100
}];

var result = [];

userData.forEach((element) => {
  if (element.id != "" && element.registrationid == "") {
    console.log("1");
    result.push(element);
  } else if (element.registrationid != "" && element.id == "") {
    console.log("2");
    element.id = element.registrationid;
    result.push(element);
  } else if (element.registrationid != "" && element.id != "") {
    console.log("3");
    if (element.id == element.registrationid) {
      console.log("4");
      result.push(element);
    } else if (element.id != element.registrationid) {
      console.log("5");
      result.push({
        ...element
      })
      element.id = element.registrationid;
      result.push(element)
    }
  }
});

console.log(result)

The assignment in 3a doesn't make sense. I removed it. I prefer flatMap

var userData = [{
  "id": 1,
  "name": "abcd",
  "place": "xyz",
  "registrationid": 100
}];

var result = userData.flatMap((element) => {
  if (element.id != "" && element.registrationid == "") {
    console.log("1");
    return element;
  } else if (element.registrationid != "" && element.id == "") {
    console.log("2");
    element.id = element.registrationid;
    return element;
  } else if (element.registrationid != "" && element.id != "") {
    console.log("3");
    if (element.id == element.registrationid) {
      console.log("4");
      return element;
    } else if (element.id != element.registrationid) {
      console.log("5");
      const oldId = element.id;
      element.id = element.registrationid;
      return [{
        ...element, id: oldId
      }, element];
    }
  }
});

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

Please try the following

  const userData = [{
    "id": 1,
    "name": "abcd",
    "place": "xyz",
    "registrationid": 100
  }];
  const result = [];

  userData.forEach((data) => {
    if (data.id && !data.registrationid) {
      result.push(data);
    }

    if (!data.id && data.registrationid) {
      const output = {
        ...data,
        id: data.registrationid
      }

      result.push(output)
    }
    
    if (data.id && data.registrationid) {
      if (data.id === data.registrationid) {
        result.push(data);
      } else {
        const obj1 = {...data, id: 1};
        const obj2 = {...data, id: 100}
        
        result.push(obj1, obj2)
      }
    }
  });
  
  console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You could create new object if you wanna do this business

var userData = [
  {
    id: 1,
    name: "abcd",
    place: "xyz",
    registrationid: 100,
  },
];

var result = [];

userData.forEach((element) => {
  let newEl;
  if (element.id != "" && element.registrationid == "") {
    newEl = { ...element };
    result.push(newEl);
  } else if (element.registrationid != "" && element.id == "") {
    newEl = { ...element, id: element.registrationid };
    result.push(newEl);
  } else if (element.registrationid != "" && element.id != "") {
    result.push(element);
    if (element.id != element.registrationid) {
      newEl = { ...element, id: element.registrationid };
      result.push(newEl);
    }
  }
});

console.log(result);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!