0

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

3
  • 1
    You can use Array.map and check whether element[0] === "err" Commented Dec 7, 2017 at 23:58
  • for (const el of data) if (el[0] != "err") el.push(0)? Commented Dec 8, 2017 at 0:01
  • I need to add nulls in the second column after I reach that index, that's where im stuck Commented Dec 8, 2017 at 0:07

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

7 Comments

This is really cool, is there a way to know the index of a[0] === "err" inside if statement?
I'd like to delete that line after this is done, tho not asked first I'd really appreciate it (:
@JaimeFH: Yes, change the loop to for (const [i, a] of data.entries) { and the i will be the index which you can use in the if where the "err" was found. Don't forget to add curly braces to the if if you're going to have more statements than is currently shown.
Couldn't get that to work, ended up creating a var like this: const idx = data.findIndex((el) => el[0] === "err") and comparing it like if (idx !== -1) ..., thanks for the help
@JaimeFH: Odd. Did you try the example I added to the answer above? It should work and it prevents you from iterating twice.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.