suchen

Heim  >  Fragen und Antworten  >  Hauptteil

javascript – So durchlaufen Sie ein Objekt und geben die Zeichenfolge des Objekts selbst aus

1

2

3

4

5

6

7

<code class="javascript">var a = {a:1,b:2}

 

var b = {a:2,b:3}

 

[a,b].forEach(key => {

    console.log(key)   

})</code>

Vielleicht ist der Ausdruck nicht ganz klar. Bitte fügen Sie ein Bild hinzu

为情所困为情所困2854 Tage vor565

Antworte allen(6)Ich werde antworten

  • PHP中文网

    PHP中文网2017-05-19 10:27:05

    1

    2

    3

    <code class="javascript">[a, b].forEach(key => {

      console.log(JSON.stringify(key))

    })</code>

    Antwort
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-19 10:27:05

    1

    2

    3

    4

    5

    6

    7

    <code>var a = {a:1,b:2};

    var b = {a:2,b:3};

    [a,b].forEach(obj => {

        for (key in obj) {

            console.log(obj[key]);

            }

        })</code>

    Antwort
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:27:05

    1

    2

    3

    4

    5

    6

    7

    8

    <code class="javascript">obj = {

        a: {},

        b: {}

    }

     

    for(key in obj) {

        console.log(key)

    }</code>

    我还是不太懂你要的结果

    Antwort
    0
  • 怪我咯

    怪我咯2017-05-19 10:27:05

    1

    2

    3

    4

    5

    <code>[a,b].forEach(obj => {

        for(key in obj) {

            console.log(key)

        }

    })</code>

    Antwort
    0
  • 黄舟

    黄舟2017-05-19 10:27:05

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    <code class="javascript">[a, b].forEach( (v) => console.log(v));

    // { a: 1, b: 2 }

    // { a: 2, b: 3 }

     

    [a, b].forEach( (v) => {

      for(key in v) {

        console.log(key)

      }

    });

    // a

    // b

    // a

    // b

     

    [a, b].forEach( (v) => {

      var {a, b} = v;

      console.log(a, b);

    });

    // 1 2

    // 2 3</code>

    Antwort
    0
  • 天蓬老师

    天蓬老师2017-05-19 10:27:05

    这样行了吧- -输出a:1 b:2 a:2 b:3

    1

    2

    3

    4

    5

    <code>        [a, b].forEach(el => {

                for (let key in el) {

                    console.log(`${key}:${el[key]}`)

                }

            });</code>

    Antwort
    0
  • StornierenAntwort