Home > Software engineering >  Reducing an array while checking/setting values
Reducing an array while checking/setting values

Time:04-02

I have some data that needs to be transformed into one array, one by one.
This is the example data (there can be a lot more squads):

var my_arr = [];
var squad1 = [
  { date: "2022-04-04", number_of_checkable_cells: 2 },
  { date: "2022-04-05", number_of_checkable_cells: 2 }
];
var squad2 = [
  { date: "2022-04-04", number_of_checkable_cells: 2 },
  { date: "2022-04-05", number_of_checkable_cells: 1 },
  { date: "2022-04-06", number_of_checkable_cells: 2 }
];

What happens: my_arr receives squad1 to add it, later squad2 joins the party.
So after squad1 is added my_arr should be:

my_arr = [
  { date: "2022-04-04", number_of_checkable_cells: 2 },
  { date: "2022-04-05", number_of_checkable_cells: 2 }
];

Then the data of squad2 is received and I want the following result:

my_arr = [
  { date: '2022-04-04', number_of_checkable_cells: 4 },
  { date: '2022-04-05', number_of_checkable_cells: 3 },
  { date: '2022-04-06', number_of_checkable_cells: 2 }
]

The code that I have:

function reduce_it(arr) {
  return arr.reduce((initiator, curr) => {
    var obj = initiator.find((i) => i.date == curr.date);
    if (obj) obj.number_of_checkable_cells  = curr.number_of_checkable_cells;
    else initiator.push(curr);
    return initiator;
  }, []);
}

my_arr = [...my_arr, ...squad1];
my_arr = reduce_it(my_arr);

my_arr = [...my_arr, ...squad2];
my_arr = reduce_it(my_arr);

Is there a better way to get to the same result? If there is, please guide me. :)

CodePudding user response:

What you suggest works, but here is what I suggest you to do. Basically, each time a new squad arrive you can loop through it and update your array of squad as follow:

var my_arr = [];
var squad1 = [
  { date: "2022-04-04", number_of_checkable_cells: 2 },
  { date: "2022-04-05", number_of_checkable_cells: 2 }
];

my_arr.push(...squad1);

// Here you received the squad 2

var squad2 = [
  { date: "2022-04-04", number_of_checkable_cells: 2 },
  { date: "2022-04-05", number_of_checkable_cells: 1 },
  { date: "2022-04-06", number_of_checkable_cells: 2 }
];

for (const squad of squad2) {
  const existingSquad = my_arr.find(
      (prevSquad) => prevSquad.date === squad.date
    );
    if (existingSquad) {
      existingSquad.number_of_checkable_cells  =
        squad.number_of_checkable_cells;
    } else {
      my_arr.push(squad);
    }
}

console.log(my_arr);
/*
(3) [{…}, {…}, {…}]
    0: {date: '2022-04-04', number_of_checkable_cells: 4}
    1: {date: '2022-04-05', number_of_checkable_cells: 3}
    2: {date: '2022-04-06', number_of_checkable_cells: 2}
length: 3
*/

So this example is the raw one, if you want to wrap it in a method look below:

function updateSquads(squads, newSquads) {
  for (const squad of newSquads) {
    const existingSquad = squads.find(
      (prevSquad) => prevSquad.date === squad.date
    );
    if (existingSquad) {
      existingSquad.number_of_checkable_cells  =
        squad.number_of_checkable_cells;
    } else {
      squads.push(squad);
    }
  }
}

var my_arr = [];
var squad1 = [
  { date: "2022-04-04", number_of_checkable_cells: 2 },
  { date: "2022-04-05", number_of_checkable_cells: 2 }
];

my_arr.push(...squad1);

// Here you receive squad 2

var squad2 = [
  { date: "2022-04-04", number_of_checkable_cells: 2 },
  { date: "2022-04-05", number_of_checkable_cells: 1 },
  { date: "2022-04-06", number_of_checkable_cells: 2 }
];
updateSquads(my_arr, squad2)
console.log(my_arr);
/*
(3) [{…}, {…}, {…}]
    0: {date: '2022-04-04', number_of_checkable_cells: 4}
    1: {date: '2022-04-05', number_of_checkable_cells: 3}
    2: {date: '2022-04-06', number_of_checkable_cells: 2}
length: 3
*/

CodePudding user response:

I would keep the target array and mutate it.

function add(target, source) {
    return source.reduce((t, o) => {
        const item = t.find(({ date }) => date == o.date);
        if (item) item.number_of_checkable_cells  = o.number_of_checkable_cells;
        else t.push({ ...o });
        return t;
    }, target);
}

const
    squad1 = [{ date: "2022-04-04", number_of_checkable_cells: 2 }, { date: "2022-04-05", number_of_checkable_cells: 2 }],
    squad2 = [{ date: "2022-04-04", number_of_checkable_cells: 2 }, { date: "2022-04-05", number_of_checkable_cells: 1 }, { date: "2022-04-06", number_of_checkable_cells: 2 }],
    my_arr = [];

add(my_arr, squad1);
console.log(my_arr);

add(my_arr, squad2);
console.log(my_arr);

  • Related