Home > Back-end >  How to create two objects from one object if fields are not same in object?
How to create two objects from one object if fields are not same in object?

Time:03-19

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

CodePudding user response:

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)

CodePudding user response:

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);

CodePudding user response:

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);

CodePudding user response:

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)
        // do not mutate data as below
        //element.id  = element.registrationid;
        // instead, create a new object and assign registrationid to id, like below
        result.push({ ...element, id: element.registrationid })
        result.push(element)
    }

}
});

console.log(result)

The problem was with mutation of the object. Here is a good article about JS object mutation.

  • Related