0

How to can I get the value of each array object iterator?
I just have some confused, anybody can help ?

let x, y, z;

x = ["a", "b", "c"].entries();
console.log("x = " + x);
// iterator [0, "a"], [1,"b"], [2,"c"]

y = ["a", "b", "c"].keys();
console.log("y = " + y);
// iterator 0, 1, 2

z = ["a", "b", "c"].values();
console.log("z = " + z);
// iterator "a", "b", "c"

// x = [object Array Iterator]
// y = [object Array Iterator]
// z = [object Array Iterator]

/*
how to can I get the value of each array object iterator?
*/

http://babeljs.io/repl/? enter image description here

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
  • 1
    If you just want to get the values out of an iterator, MDN has a good summary: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols – apsillers Oct 21 '16 at 14:15
  • I don't understand what you mean by *"value of each array object iterator"*. Do you want to consume the iterators? – Felix Kling Oct 21 '16 at 15:25

2 Answers2

3

You can use Array.from() to convert an iterator object to an array:

const arr = ['a', 'b', 'c']

console.log(Array.from(arr.keys()))

See also Iteration protocols on MDN.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
0

Here is my solution!

you can follow my steps if you get a obstruct!

https://github.com/gildata/RAIO/issues/61

steps

  1. objects to array, {{},{}} => arr = [{},{}];

for in & key

  1. get object's title , title = key;

for in & key

  1. create a new object obj = {title, arr};

Object.assign(obj, {new_key: "new value"});

  1. wrapped array , array.push(obj);

for of / for in

  1. just use the wrapped array

array.map() & Table

    // fetch data by `ReportName` & `GetRowSchema`
    outputClick = () => {
        fetch(`https://cdn.xgqfrms.xyz/json/tables.json`)
        .then((response) => response.json())
        .then((json)=> {
            console.log(`json = ${json}`);
            console.log(`json.length = ${json.length}`);
            console.log(`json.Info`, json.Info.schema.Properties);
            // Properties
            let datas = [];
            if(json.Info.schema.Properties !== undefined){
                // datas.BasicInformationRow.Properties
                datas = json.Info.schema.Properties;
            }else{
                let tables = json.Info.schema;
                // let i = 0;
                for (let key in tables) {
                    let arr = [];
                    let new_obj = {};
                    let i = 0;
                    if(!tables.hasOwnProperty(key)) continue;
                    if (tables.hasOwnProperty(key)) {
                        let title = tables[key].desc,
                            objs = tables[key].Properties;
                        for (let key in objs) {
                            if (objs.hasOwnProperty(key)) {
                                // A0
                                objs[key].name =  key;
                                objs[key].key = ("k000" + i++);
                            }
                            arr.push(objs[key]);
                            console.log(`arr ${key}`, arr);
                        }
                        console.log(`title ${key}`, title);
                        new_obj.title = tables[key].desc;
                        new_obj.datas = arr;
                        console.log(`new obj = `, new_obj);
                    }
                    datas.push(new_obj);
                    const css = `
                        color: #0f0;
                        font-size: 23px;
                    `;
                    const css2 = `
                        color: #f00;
                        font-size: 16px;
                    `;
                    console.log(`%c datas key = ${key} \n datas = `, css, datas);
                    console.log(`%c datas i = ${i} \n datas = `, css2, datas[i]);
                    // i++;
                }
            }
            console.log(`datas[0] = `, datas[0]);
            console.log(`datas[0].length = `, datas[0].length);
            // Array.isArrray(datas[0]);
            console.log(`Array.isArray(datas[0]) = `, Array.isArray(datas[0]));
            console.log(`typeof datas[0] = `, typeof(datas[0]));
            this.setState(
                {
                    output_datas: datas
                }
            );
            return datas;
        });
    };
  • https://stackoverflow.com/questions/5708784/how-do-i-choose-a-random-value-from-an-array-with-javascript –  Aug 10 '17 at 07:05