Javascript,获取对象数组的过去值

2guxujil  于 2023-02-07  发布在  Java
关注(0)|答案(3)|浏览(133)

我有一个JavaScript对象数组,看起来像

var myarr = [
{'xx':'2023-01-01,,1'},
{'ss':'2023-01-01,2,1.2'},
{'dd':'2023-01-01,4,'},
{'rr':'2023-01-01,,'},
{'ff':'2023-01-01,,'},
{'gg':'2023-01-01,,'}
];

这个数组实际上要比这个大得多,但是出于测试的目的,我将它缩小了,我的一些数组有数千行长
每个对象包含一个日期和两个逗号分隔的值,尽管我有一些行包含3或4个逗号分隔的值
我需要做的是,如果在任何行中发现任何空白逗号分隔值,则从该位置获取上一个逗号分隔值,最多返回2次,不过将来可能需要将其更改为更大的值
因此,对于我的示例,我将得到以下输出

var myarr = [
{'xx':'2023-01-01,,1.6'},
{'ss':'2023-01-01,2,1.2'},
{'dd':'2023-01-01,4,1.2'},
{'rr':'2023-01-01,4,1.2'},
{'ff':'2023-01-01,4,'},
{'gg':'2023-01-01,,'}
];

我试过用

var myarr = [
{'xx':'2023-01-01,,1'},
{'ss':'2023-01-01,2,1.2'},
{'dd':'2023-01-01,4,'},
{'rr':'2023-01-01,,'},
{'ff':'2023-01-01,,'},
{'gg':'2023-01-01,,'}
];

var maxAttempts = 3;

for (var i = 0; i < myarr.length; i++) {
  var obj = myarr[i];
  var values = Object.values(obj)[0].split(",");
  var date = values[0];
  var value1 = values[1];
  var value2 = values[2];
  for (var j = 1; j <= maxAttempts; j++) {
    if (!value1) {
      value1 = (myarr[i-j] && Object.values(myarr[i-j])[0].split(",")[1]) || " ";
    }
    if (!value2) {
      value2 = (myarr[i-j] && Object.values(myarr[i-j])[0].split(",")[2]) || " ";
    }
    if (value1 && value2) {
      break;
    }
  }
  console.log(date, value1, value2);
  for (var k = 3; k < values.length; k++) {
    var value = values[k];
    console.log(value);
  }
}

但它似乎没有提供预期的输出。
有人能帮我解决一下可能出了什么问题吗?

gz5pxeao

gz5pxeao1#

也许你可以用这个。

const myarr = [
    { "xx": "2023-01-01,,1" },
    { "ss": "2023-01-01,2,1.2" },
    { "dd": "2023-01-01,4," },
    { "rr": "2023-01-01,," },
    { "ff": "2023-01-01,," },
    { "gg": "2023-01-01,," }
]

function fillInBlanks(arr, maxLookBack) {
    return arr.map((obj, index) => {
        const key = Object.keys(obj)[0]
        const value = Object.values(obj)[0]
            .split(",")
            .map((x, n) => {
                if (x === "" && index > 0) {
                    for (let i = index - 1; i >= Math.max(0, index - maxLookBack); --i) {
                        const prev = Object.values(arr[i])[0].split(",")
                        if (prev[n] !== "") return prev[n]
                    }
                } else return x
            })
        return Object.fromEntries([
            [key, value.join(",")]
        ])
    })
}

fillInBlanks(myarr, 2).forEach(x => console.log(x))
v440hwme

v440hwme2#

下面是我的尝试。这也适用于每行任意数量的值。

const maxAttempts = 2;

myarr.reduce((modifiedAccumulation, currentObject, index) => {
  const [key, csv] = Object.entries(currentObject)[0];
  const splitCsv = csv.split(",");
  const modifiedCsv = splitCsv
    .reduce((fixedArray, currentElement, csvPos) => {
      let numberToUse =
        currentElement === ""
          ? myarr
              .slice(Math.max(index - maxAttempts, 0), index)
              .reduceRight((proposedNum, currentPastObj) => {
                if (proposedNum !== "") return proposedNum;
                let candidate =
                  Object.entries(currentPastObj)[0][1].split(",")[csvPos];
                return candidate !== "" ? candidate : "";
              }, "")
          : currentElement;

      return [...fixedArray, numberToUse];
    }, [])
    .join(",");
  return [...modifiedAccumulation, { [key]: modifiedCsv }];
}, []);
mklgxw1f

mklgxw1f3#

这种方法创建了一个包含最后几个条目的“window”数组,用于查找以前的列值。

const myarr = [{"xx":"2023-01-01,,1"},{"ss":"2023-01-01,2,1.2"},{"dd":"2023-01-01,4,"},{"rr":"2023-01-01,,"},{"ff":"2023-01-01,,"},{"gg":"2023-01-01,,"}]

const windowSize = 2

const w = [], r =
  myarr.map(e=>Object.entries(e).flatMap(([k,v])=>[k,...v.split(',')]))
  .map(a=>(
    w.unshift(a) > windowSize+1 && w.pop(),
    a.map((_,i)=>w.find(x=>x[i])?.[i])
  )).map(([k,...v])=>[k,v.join()]
).map(i=>Object.fromEntries([i]))

console.log(r)

相关问题