0

I found code:

var B = [];
var data = [];
data.push("string");
// ....
B.push(data);
// ... 
for each (var A in B){
    console.log(B);
    console.log(A);
    let obj = A[0].split("|", 3);
    console.log(objid[0]);
    console.log(objid[1]);
}

So B is an array of array, I printed B, it is like:

[
   [
      "1+!|6789|1234",
      "15:00"
   ],
   [
      "2+!|1234|4567",
      "16:00"
   ]
]

And I also printed obj:

["!1+", "6789", "1234"]
["2+!", "1234", "4567"]

which seems correct. And I run this code, it works fine and all functionality works well. But my VScode complains it has syntax error, and I read this:

https://developer.mozilla.org/en-US/docs/Archive/Web/JavaScript/for_each...in

So I tried

  1. remove each and use of
  2. remove each and keep using in
  3. keep each and use of

for 1, it just crashed and give me error: SyntaxError: missing ; after for-loop initializer

for 2, I tried to print A, and I got A is 0, which is obviously wrong.

for 3, it just crashed and give me error: SyntaxError: invalid for each loop

So how should I change it? I guess the old code is correct just deprecated, and I need a replacement that works exact the same way as it. Thanks!

rioV8
  • 24,506
  • 3
  • 32
  • 49
coder2
  • 37
  • 5
  • For 2: `for (var a in b)` sets `a` to the indexes, not the values. – Barmar Sep 02 '20 at 18:48
  • @Barmar yes, so "for each(var a in b)" sets `a` to the values? Seems yes, weird usage.. – coder2 Sep 02 '20 at 18:50
  • @Barmar, then why `for (var a of b)` did not work? – coder2 Sep 02 '20 at 18:52
  • It should work. Are you sure you had VSCode set to allow EcmaScript 6 syntax? – Barmar Sep 02 '20 at 18:53
  • @Barmar, you are right.. ES6 is enabled in my group, is there a way to fix it with ES5? – coder2 Sep 02 '20 at 19:26
  • `for-of` is a new ES6 feature. – Barmar Sep 02 '20 at 19:27
  • @Barmar, thanks.. but is there a way to fix it with ES5? – coder2 Sep 02 '20 at 19:32
  • Use `for (var A in B)` and then use `B[A]` to get the element value. – Barmar Sep 02 '20 at 19:34
  • But read https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea?lq=1 – Barmar Sep 02 '20 at 19:35
  • @Barmar Thanks for the link. So I was thinking, in my case, if i have to find some way to replace `for each(var a in b)`, and to avoid `for..in` as you post, maybe I should use `var index; for (index =0; index < B.length; index++)` and then `var A = B[index]`? In this way, it's longer but seems safer than `for (var A in B)`..? – coder2 Sep 02 '20 at 19:48
  • Yes, that's the preferred way. – Barmar Sep 02 '20 at 19:49
  • @Barmar Thanks for the reply. But I just realize -- if i want to replace equally for `for each (var a in b)`, then i should use `for (var A in B)` then `B[A]` right? ... I mean, the code I posted in my last comment is better and preferred, but `for (var A in B)` is the equally replacement right? :) – coder2 Sep 02 '20 at 19:52
  • Yes, that's what I said in my comment right before the link. – Barmar Sep 02 '20 at 19:56
  • @Barmar Thanks so much! Appreciate it! But I am still bit confused about `for each...in`, I understand it iterates over property values(like in this case, it iterates values of the array), but in this link: https://developer.mozilla.org/en-US/docs/Archive/Web/JavaScript/for_each...in I noticed it says "Never use a loop like this on arrays" but apparently it worked on arrays(like in my case it iterates all the values)...Does it mean `for each.. in` works for arrays just not recommended? – coder2 Sep 02 '20 at 20:03
  • Yes, that's what it means. But it's deprecated so you shouldn't use it at all. This is the first time I've even seen it. – Barmar Sep 02 '20 at 20:09
  • It's more than just deprecated, it has been completely removed from recent browser versions. – Barmar Sep 02 '20 at 20:10
  • @Barmar thanks! Actually i was confused with it saying "Never use a loop like this on arrays. Only use it on objects"... In my mind, it should work for both array and objects..So I was a bit confused.. but yes, no matter it's object or array, we should not use it at all – coder2 Sep 02 '20 at 20:23

3 Answers3

0

Try this

for (const A of B) {
    console.log(B);
    console.log(A);
    let obj = A[0].split("|", 3);
    console.log(objid[0]);
    console.log(objid[1]);
}
0

It's an array. Use forEach:

B.forEach(A => {
  console.log(B);
  console.log(A);
  let obj = A[0].split("|", 3);
  console.log(obj[0]);
  console.log(obj[1]);
});
ray
  • 26,557
  • 5
  • 28
  • 27
0
  • For...of

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.

for (variavel of iteravel) {
  console.log(element);
}
  • For...in

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
  console.log(`${property}: ${object[property]}`);
}
  • For each...in (DEPRECATED)

The for each...in statement is deprecated as the part of ECMA-357 (E4X) standard. E4X support has been removed. Consider using for...of instead.

  • forEach

The forEach() method executes a provided function once for each array element.

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));
  • for

The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.

let str = '';

for (let i = 0; i < 9; i++) {
  str = str + i;
}

console.log(str);
// expected output: "012345678"