Jaime FH
Jaime FH

Reputation: 148

transform array of arrays inserting nulls, Javascript

I am receiving data in this way:

  [Sun Jan 01 2006 00:00:00 GMT-0600 (CST), 8.605613643557573]
  [Mon Jan 01 2007 00:00:00 GMT-0600 (CST), 4.639263458390814]
  [Tue Jan 01 2008 00:00:00 GMT-0600 (CST), 3.690424190442011]
  [Thu Jan 01 2009 00:00:00 GMT-0600 (CST), -6.068362055704255]
  [Fri Jan 01 2010 00:00:00 GMT-0600 (CST), 0.011317380895792274]
  [Sat Jan 01 2011 00:00:00 GMT-0600 (CST), 3.9984661908088825]
  [Sun Jan 01 2012 00:00:00 GMT-0600 (CST), 2.4211622308876173]
  [Tue Jan 01 2013 00:00:00 GMT-0600 (CST), -1.5740599423273804]
  [Wed Jan 01 2014 00:00:00 GMT-0600 (CST), 2.6624793033769967]
  [Thu Jan 01 2015 00:00:00 GMT-0600 (CST), 1744.9379869455643]
  ["err", NaN]
  [Wed Jan 01 2020 00:00:00 GMT-0600 (CST), 3417.1886283181875]
  [Wed Jan 01 2025 00:00:00 GMT-0600 (CST), 3331.7059850696196]
  [Tue Jan 01 2030 00:00:00 GMT-0600 (CST), 3237.940431993671]

And I'm trying to make a dataset to look like this:

  [Sun Jan 01 2006 00:00:00 GMT-0600 (CST), 8.605613643557573, 0]
  [Mon Jan 01 2007 00:00:00 GMT-0600 (CST), 4.639263458390814, 0]
  [Tue Jan 01 2008 00:00:00 GMT-0600 (CST), 3.690424190442011, 0]
  [Thu Jan 01 2009 00:00:00 GMT-0600 (CST), -6.068362055704255, 0]
  [Fri Jan 01 2010 00:00:00 GMT-0600 (CST), 0.011317380895792274, 0]
  [Sat Jan 01 2011 00:00:00 GMT-0600 (CST), 3.9984661908088825, 0]
  [Sun Jan 01 2012 00:00:00 GMT-0600 (CST), 2.4211622308876173, 0]
  [Tue Jan 01 2013 00:00:00 GMT-0600 (CST), -1.5740599423273804, 0]
  [Wed Jan 01 2014 00:00:00 GMT-0600 (CST), 2.6624793033769967, 0]
  [Thu Jan 01 2015 00:00:00 GMT-0600 (CST), 1744.9379869455643, 0]
  ["err", NaN]
  [Wed Jan 01 2020 00:00:00 GMT-0600 (CST),0 , 3417.1886283181875]
  [Wed Jan 01 2025 00:00:00 GMT-0600 (CST),0 , 3331.7059850696196]
  [Tue Jan 01 2030 00:00:00 GMT-0600 (CST),0 , 3237.940431993671]

Is there a way to do this by looking at the index of "err" ?

Upvotes: 0

Views: 72

Answers (2)

user8897421
user8897421

Reputation:

You can use a variable to track if "err" has been found, and use that information to decide where to .splice() the new member.

var found = false
for (const a of data) {
  if (!found && a[0] === "err")
    found = true;
  else
    a.splice(found ? 1 : 2, 0, 0);
}

If you want to make use of the index where the err was found, then use .entries() on the array and change the found variable to store the index instead.

var foundIdx = -1;
for (const [idx, a] of data.entries()) {
  const found = foundIdx !== -1;

  if (!found && a[0] === "err")
    foundIdx = idx;
  else
    a.splice(found ? 1 : 2, 0, 0);
}

console.log("the err index was", foundIdx);

Upvotes: 2

Bergi
Bergi

Reputation: 664433

You can look at the index of the error using findIndex:

var errIndex = data.findIndex(el => el[0] == "err");
assert(errIndex != -1);
return data.map(([date, val], i) => {
  if (i < errIndex)
    return [date, val, 0];
  if (i > errIndex)
    return [date, 0, val];
  return ["err", NaN] // or whatever
);

Alternatively you might want to do

var it = data.values();
for (const el of it) {
  if (el[0] == "err") break;
  el.push(0);
}
for (const el of it) {
  el.splice(1, 0, 0);
}

Upvotes: 0

Related Questions