制作新数组,这是其他两个的组合

Make New Array which is combination of other Two

我有两个数组,一个是普通数组,其中存储日期值

    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"
]

另一个是存在两个对象的数组对象

    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"
  }
]

我的要求是将新数组转换成这样

[
{
    "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"
}

]

就像对象 2022-06-11 中的数据一样,所以新数组将包含一个对象,该对象将具有与该对象相关的属性。类似于对象是否存在 start_date 也存在于日期数组中,因此新数组将包含这些信息的对象。否则它将创建对应于日期的空白对象。

但我可以用这个

[
{
    "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"
}

]

它只接受第一个条目,打印和其余部分都是空白的。

我的代码:-

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

有人可以帮我解决这个问题吗?提前致谢。

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

只需遍历 A 中的日期并在 B 中找到匹配项。然后创建一个包含所需成员的对象并将其推送到结果数组。

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

您可能也想为这两个原始对象创建接口,但我认为这应该对您有所帮助。

这是 Playground 中的结果。