如何分组javascript并检测是否存在一个

时间:2019-02-11 17:19:22

标签: javascript

我想每天检查此输入中是否存在特定代码:

const arrayA = [
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/10", "code":"B"},
{"date":"2018/9/10", "code":"C"},
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/11", "code":"A"},
{"date":"2018/9/11", "code":"C"}];

规则:如果当天有B,那么我们加X 如果当天有C,那么我们加Y 如果当天有B&C,我们加Y

我在使用filter和。一些代码时迷失了自我。

预期产量

   const arrayOut = [
    {"date":"2018/9/10", "code":"A"},
    {"date":"2018/9/10", "code":"B"},
    {"date":"2018/9/10", "code":"C"},
    {"date":"2018/9/10", "code":"A"},
    {"date":"2018/9/10", "code":"Y"},
    {"date":"2018/9/11", "code":"A"},
    {"date":"2018/9/11", "code":"B"},
    {"date":"2018/9/11", "code":"X"}];

const arrayA = [
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/10", "code":"B"},
{"date":"2018/9/10", "code":"C"},
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/11", "code":"B"},
{"date":"2018/9/11", "code":"B"}];

var helper = [];
arrayA.map ( e => {
  var key = e.date;
  if(!helper[key]){
     helper.push(key);
     var arr = arrayA.filter(e => e.date == key && (e.code == "B" || e.code == "C"));

      if(arr.length > 0) console.log("act is here");
      else console.log("act not here");
	}
  
  });

3 个答案:

答案 0 :(得分:3)

我个人将其映射到一个对象,这样您就可以进行简单的布尔检查,而不仅仅是将项目添加到列表中。

const arrayA = [
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/10", "code":"B"},
{"date":"2018/9/10", "code":"C"},
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/11", "code":"B"},
{"date":"2018/9/11", "code":"B"}];

const state = arrayA.reduce( (obj, state) => {
  obj[state.date] = obj[state.date] || {}
  obj[state.date][state.code] = true
  return obj
}, {});

Object.entries(state).forEach(([date, codes]) => {
  if (codes.C) {
    arrayA.push({ date, code: 'Y' })
  } else if (codes.B) {
    arrayA.push({ date, code: 'X' })
  }
})

console.log(arrayA)


更新以在不使用Object.entries()的情况下进行处理:

const arrayA = [
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/10", "code":"B"},
{"date":"2018/9/10", "code":"C"},
{"date":"2018/9/10", "code":"A"},
{"date":"2018/9/11", "code":"B"},
{"date":"2018/9/11", "code":"B"}];

const state = arrayA.reduce( (obj, state) => {
  obj[state.date] = obj[state.date] || {}
  obj[state.date][state.code] = true
  return obj
}, {});

for (date in state)
{
  if (state[date].C)
    arrayA.push({ date, code: 'Y' });
  else if (state[date].B)
    arrayA.push({ date, code: 'X' });
}

console.log(arrayA);

答案 1 :(得分:2)

另一种解决方案是首先在日期和与其相关的代码集之间创建一个映射。完成此任务后,您可以在地图上循环,并根据date与之关联的代码集在原始arrayA上插入新元素:

const arrayA = [
  {"date":"2018/9/10", "code":"A"},
  {"date":"2018/9/10", "code":"B"},
  {"date":"2018/9/10", "code":"C"},
  {"date":"2018/9/10", "code":"A"},
  {"date":"2018/9/11", "code":"B"},
  {"date":"2018/9/11", "code":"B"}
];

// Create map between dates and set of codes they have.

let dateMap = arrayA.reduce((map, {date, code}) =>
{
    if (map.has(date))
        map.set(date, map.get(date).add(code));
    else
        map.set(date, new Set(code));

    return map;
}, new Map());

// Now, loop on the map to check what elements to add.

dateMap.forEach((codes, date) =>
{
    if (codes.has("C") && codes.has("B"))
        arrayA.push({date, code: "Y"});
    else if (codes.has("C"))
        arrayA.push({date, code: "Y"});
    else if (codes.has("B"))
        arrayA.push({date, code: "X"});
});

console.log("Unsorted: ", arrayA);

// Now, if you need, you can sort the array.

arrayA.sort(
    (a, b) => a.date.localeCompare(b.date) || a.code.localeCompare(b.code)
);

console.log("Sorted: ", arrayA);

答案 2 :(得分:0)

您可以在地图中收集数据,稍后再检查所需代码。

const
    array = [{ date: "2018/9/10", code: "A"}, { date: "2018/9/10", code: "B" }, { date: "2018/9/10", code: "C" }, { date: "2018/9/10", code: "A" }, { date: "2018/9/11", code: "A" }, { date: "2018/9/11", code: "B" }],
    result = Array
        .from(array.reduce((m, o) => m.set(o.date, [...(m.get(o.date) || []), o]), new Map).values())
        .reduce((r, a) => {
            var code;
            if (a.some(({ code }) => code === 'B')) code = 'X';
            if (a.some(({ code }) => code === 'C')) code = 'Y';
            if (code) a.push({ date: a[0].date, code });
            return r.concat(a);
        }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }