Home > Back-end >  Map array based in another
Map array based in another

Time:11-17

I have an array of objects, and I need to generate other based on it.

But, if the new arrays don't have the property, I need to generate a default value based on first array.

  const dailyLabels = [
    "01/11",
    "02/11",
    "03/11",
    "04/11"
  ]
  
  const dailyCurr = [
    {
      mov_date: "2022-11-03T14:16:10.694Z",
      value: 2,
      cod: 5,
      product: "ODONTO",
      day: "03/11",
      year: "curr"
    },
    {
      mov_date: "2022-11-04T14:16:10.694Z",
      value: 2,
      cod: 5,
      product: "ODONTO",
      day: "04/11",
      year: "curr"
    }
  ]

So, I need to generate a third array with the missing days between dailyLabels and dailyCurr, I expect something like this:

const thirdArray = [
    {
      mov_date: "",
      value: 0,
      cod: null,
      product: "",
      day: "01/11",
      year: "curr"
    },
    {
      mov_date: "",
      value: 0,
      cod: null,
      product: "",
      day: "02/11",
      year: "curr"
    },
    {
      mov_date: "2022-11-03T14:16:10.694Z",
      value: 2,
      cod: 5,
      product: "ODONTO",
      day: "03/11",
      year: "curr"
    },
    {
      mov_date: "2022-11-04T14:16:10.694Z",
      value: 2,
      cod: 5,
      product: "ODONTO",
      day: "04/11",
      year: "curr"
    }
  ]

CodePudding user response:

Use Array.map() and Array.find() to find the objects that have a matching day value, and add objects for the remaining ones.

const dailyLabels = ["01/11", "02/11", "03/11", "04/11"];

const dailyCurr=[{mov_date:"2022-11-03T14:16:10.694Z",value:2,cod:5,product:"ODONTO",day:"03/11",year:"curr"},{mov_date:"2022-11-04T14:16:10.694Z",value:2,cod:5,product:"ODONTO",day:"04/11",year:"curr"}];

const thirdArr = dailyLabels.map(day => {
  const newObj = dailyCurr.find(obj => obj.day === day); 
  if(!newObj) {
    return {
      mov_date: "",
      value: 0,
      cod: null,
      product: "",
      day: day,
      year: "curr"
    };
  }else{
    return newObj;
  }
});

console.log(thirdArr);

CodePudding user response:

Hmmm, if I understand correctly you need to:

  1. check if value exists in array of object
  2. If value doesn't exist then push object with default values

It should be quite simple with simple forEach and if statement. I bet this could be done with "fancy one line syntax", but for simplicity I will provide forEach approach.

const dailyLabes  = [ "01/11", "02/11", "03/11", "04/11" ]

const dailyCurr = [ { mov_date: "2022-11-03T14:16:10.694Z", value: 2, cod: 5, product: "ODONTO", day: "03/11", year: "curr" }, { mov_date: "2022-11-04T14:16:10.694Z", value: 2, cod: 5, product: "ODONTO", day: "04/11", year: "curr" } ]

// Deep copy
const thirdArr = [...dailyCurr];

dailyLabes.forEach(label => {
  if (!dailyCurr.find(dateObj => dateObj.day === label)) {
     thirdArr.push({ mov_date: "", value: 0, cod: 0, product: "", day: label, year: "curr" });
  }
});

You can also sort array if you want.

  • Related