Home > Blockchain >  Make New Array which is combination of other Two
Make New Array which is combination of other Two

Time:05-21

I Have Two Array, One is normal one where dates values are being stored

    var = [
    "2022-05-01",
    "2022-05-02",
    "2022-05-03",
    "2022-05-04",
    "2022-05-05",
    "2022-05-06",
    "2022-05-07",
    "2022-05-08",
    "2022-05-09",
    "2022-05-10",
    "2022-05-11",
    "2022-05-12",
    "2022-05-13",
    "2022-05-14",
    "2022-05-15",
    "2022-05-16",
    "2022-05-17",
    "2022-05-18",
    "2022-05-19",
    "2022-05-20",
    "2022-05-21",
    "2022-05-22",
    "2022-05-23",
    "2022-05-24",
    "2022-05-25",
    "2022-05-26",
    "2022-05-27",
    "2022-05-28",
    "2022-05-29",
    "2022-05-30"
]

The other one is objects of Array where two objects are present

    var b = [
  {
    "k_id": "6dcb67eb-1c8a-4239-9446-f9d8f6a68086",
    "v_id": "aacc1765-a1d3-43c3-8233-beae19d258e5",
    "key": "Testing",
    "value": "999",
    "start_date": "2022-05-06T00:00:00.000Z",
    "end_date": "2022-05-06T00:00:00.000Z"
  },
  {
    "k_id": "6dcb67eb-1c8a-4239-9446-f9d8f6a68086",
    "v_id": "ad95cc4a-ec72-4d6e-a452-f519358f265d",
    "key": "Testing",
    "value": "189",
    "start_date": "2022-05-08T00:00:00.000Z",
    "end_date": "2022-05-`enter code here`08T00:00:00.000Z"
  }
]

My Requirement is to Convert new array into like this

[
{
    "value": "",
    "value_id": "",
    "date": "2022-05-01"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-02"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-03"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-04"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-05"
},
{
    "value": "999",
    "value_id": "48138929-6848-4ffe-86ce-183c75d021b4",
    "date": "2022-05-06"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-07"
},
{
    "value": "189",
    "value_id": "06c4f66f-efae-4981-bebd-6734addab4a5",
    "date": "2022-05-08"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-09"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-10"
},
{
    "value": "",
    "value_id": "",
    "date": "2022-05-11"
}

]

Like in the object 2022-06-11 data is there, so the new array will consist an object which will have properties related to that object. Similar to if the object has start_date present which is also in date array, so the new array will consist object for those info. Otherwise it will create blank object respective to date.

But I am able to come with this

[
{
    "value": "999",
    "value_id": "aacc1765-a1d3-43c3-8233-beae19d258e5",
    "date": "2022-05-06"
},
{
    "value": "189",
    "value_id": "ad95cc4a-ec72-4d6e-a452-f519358f265d",
    "date": "2022-05-08"
},
{
    "value": "",
    "date": "2022-05-03"
},
{
    "value": "",
    "date": "2022-05-04"
},
{
    "value": "",
    "date": "2022-05-05"
},
{
    "value": "",
    "date": "2022-05-06"
},
{
    "value": "",
    "date": "2022-05-07"
},
{
    "value": "",
    "date": "2022-05-08"
},
{
    "value": "",
    "date": "2022-05-09"
},
{
    "value": "",
    "date": "2022-05-10"
},
{
    "value": "",
    "date": "2022-05-11"
},
{
    "value": "",
    "date": "2022-05-12"
},
{
    "value": "",
    "date": "2022-05-13"
},
{
    "value": "",
    "date": "2022-05-14"
},
{
    "value": "",
    "date": "2022-05-15"
},
{
    "value": "",
    "date": "2022-05-16"
},
{
    "value": "",
    "date": "2022-05-17"
},
{
    "value": "",
    "date": "2022-05-18"
},
{
    "value": "",
    "date": "2022-05-19"
},
{
    "value": "",
    "date": "2022-05-20"
},
{
    "value": "",
    "date": "2022-05-21"
},
{
    "value": "",
    "date": "2022-05-22"
},
{
    "value": "",
    "date": "2022-05-23"
},
{
    "value": "",
    "date": "2022-05-24"
},
{
    "value": "",
    "date": "2022-05-25"
},
{
    "value": "",
    "date": "2022-05-26"
},
{
    "value": "",
    "date": "2022-05-27"
},
{
    "value": "",
    "date": "2022-05-28"
},
{
    "value": "",
    "date": "2022-05-29"
},
{
    "value": "",
    "date": "2022-05-30"
}

]

It is taking only first entry and printing and rest are coming as blank.

My Code: -

var a = {};
          dateArr = [];
          for (var j = 0; j < this.date_val.length; j  ) {
            debugger;
              var b = {}
              console.log(val2.value[j]);
              
              if (val2.value.length > 0) {
                const newKey = this.date_val[j];
                b[newKey] = val1.data;
                // if (
                //   moment(val2.value[j].start_date).format("YYYY-MM-DD") !=
                //   this.date_val[j]
                // ) {
                //   this.vala = val2.value[j].value;
                //   this.valb = val2.value[j].val_id;
                //   this.valc = moment(val2.value[j].start_date).format(
                //     "YYYY-MM-DD"
                //   );

                // } 

                // if (
                //   moment(val2.value[j].start_date).format("YYYY-MM-DD") !=
                //   this.date_val[j]
                // ) {
                  a = {
                    value:
                      val2.value[j] != undefined ? val2.value[j].value : "",
                    value_id:
                      val2.value[j] != undefined
                        ? val2.value[j].v_id
                        : undefined,
                    date:
                      val2.value[j] != undefined
                        ? moment(val2.value[j].start_date).format(
                            "YYYY-MM-DD"
                          )
                        : this.date_val[j],
                  };
                // }
                
              } else {
                a = {
                  value: "",
                  value_id: undefined,
                  date: this.date_val[j],
                };
              }
            dateArr.push(a);
          }
          this.nameArray.push({
            key: val1.key,
            key_val: val1.id,
            date: dateArr,
          });

Can someone please help me with this? Thanks in Advance.

CodePudding user response:

var a = [
  "2022-05-01",
  "2022-05-02",
  "2022-05-03",
  "2022-05-04",
  "2022-05-05",
  "2022-05-06",
  "2022-05-07",
  "2022-05-08",
  "2022-05-09",
  "2022-05-10",
  "2022-05-11",
  "2022-05-12",
  "2022-05-13",
  "2022-05-14",
  "2022-05-15",
  "2022-05-16",
  "2022-05-17",
  "2022-05-18",
  "2022-05-19",
  "2022-05-20",
  "2022-05-21",
  "2022-05-22",
  "2022-05-23",
  "2022-05-24",
  "2022-05-25",
  "2022-05-26",
  "2022-05-27",
  "2022-05-28",
  "2022-05-29",
  "2022-05-30",
];

var b = [
  {
    k_id: "6dcb67eb-1c8a-4239-9446-f9d8f6a68086",
    v_id: "aacc1765-a1d3-43c3-8233-beae19d258e5",
    key: "Testing",
    value: "999",
    start_date: "2022-05-06T00:00:00.000Z",
    end_date: "2022-05-06T00:00:00.000Z",
  },
  {
    k_id: "6dcb67eb-1c8a-4239-9446-f9d8f6a68086",
    v_id: "ad95cc4a-ec72-4d6e-a452-f519358f265d",
    key: "Testing",
    value: "189",
    start_date: "2022-05-08T00:00:00.000Z",
    end_date: "2022-05-08T00:00:00.000Z",
  },
];

let results = a.map((dateString, idx) => {
  // You'll want to parse the time, many ways
  // to do this, then compare times. This is
  // hardcoded to work for this exact format
  let bWithMatchedIndices = b.find(
    ({ start_date }) => dateString === start_date.split("T")[0]
  );

  if (bWithMatchedIndices?.start_date.split("T")[0] === dateString) {
    return {
      date: dateString,
      value: bWithMatchedIndices.value,
      value_id: bWithMatchedIndices.v_id,
    };
  } else {
    return {
      date: dateString,
      value: "",
      value_id: "",
    };
  }
});
console.log(results);

CodePudding user response:

Just loop over the dates in A and find a match in B. Then create an object with the members you need and push it to the resulting array.

interface NewItem {
    date: string,
    value: string,
    value_id: string
}

function transform(a: any[], b: any[]): NewItem[] {
    let newArray: NewItem[] = []

    a.forEach(item => {
        const objectFound = b.find(x => x.start_date.split('T')[0] === item)

        if (objectFound) {
            newArray.push({ date: item, value: objectFound.value, value_id: objectFound.v_id })
        } else {
            newArray.push({ date: item, value: '', value_id: '' })
        }
    })

    return newArray
}


console.log(transform(a, b))

You might want to create interfaces for the two original objects too, but I think this should help you on your way just fine.

Here's the result in a Playground.

  • Related