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

2
  • 1
    If you just want to get the values out of an iterator, MDN has a good summary: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 21, 2016 at 14:15
  • I don't understand what you mean by "value of each array object iterator". Do you want to consume the iterators? Commented Oct 21, 2016 at 15:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.