Flatten object to array?

前端 未结 7 1222
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 03:21

I\'m using an object as a hash table. I\'d like to quickly print out its contents (for alert() for instance). Is there anything built in to convert a hash into

相关标签:
7条回答
  • 2021-01-05 03:42

    for quick & dirty use in alert you could use JSON:

    alert(JSON.stringify(yourObj).replace(/,/g,'\n'));
    
    0 讨论(0)
  • 2021-01-05 03:43

    Maybe a little late, but here you have my version of the answer, updated to ES2015. I use a recursive function and it works even if there are other objects inside the main object:

    function objectFlattener (object) {
      return Reflect.apply(Array.prototype.concat, [], Object.keys(object).map(key => {
        if (object[key] instanceof Object) {
          return objectFlattener(object[key]);
        }
        return `${ key }: ${ object[key] }`;
      }));
    }
    

    So changing the last return you can format the element inside your array.

    0 讨论(0)
  • 2021-01-05 03:44

    Not that I'm aware of. Still, you can do it yourself fairly concisely:

    var obj = { a: 1, b: 2, c: 3 };
    var arr = [];
    for (var i in obj) {
       var e = {};
       e[i] = obj[i];
       arr.push(e);
    }
    console.log(arr);
    // Output: [Object { a=1 }, Object { b=2 }, Object { c=3 }]
    

    Of course, you can't alert this either, so you might as well just console.log(obj) in the first place.


    You could output arrays of arrays:

    var obj = { a: 1, b: 2, c: 3 };
    var arr = [];
    for (var i in obj) {
       arr.push([i, obj[i]]);
    }
    console.log(arr);
    // Output: [["a", 1], ["b", 2], ["c", 3]]
    
    alert(arr);
    // Alert: a, 1, b, 2, c, 3
    

    But, again, ew.

    0 讨论(0)
  • 2021-01-05 03:45

    Here is my version of it. It should allow you to flatten input like below:

    var input = {
       a: 'asdf',
       b: [1,2,3],
       c: [[1,2],[3,4]],
       d: {subA: [1,2]}
    }
    

    The function is like this:

        function flatten (input, output) {
    
          if (isArray(input)) {
            for(var index = 0, length = input.length; index < length; index++){
              flatten(input[index], output);
            }
          }
          else if (isObject(input)) {
            for(var item in input){
              if(input.hasOwnProperty(item)){
                flatten(input[item], output);
              }
            }
          }
          else {
            return output.push(input);
          }
        };
    
        function isArray(obj) {
          return Array.isArray(obj) || obj.toString() === '[object Array]';
        }
    
        function isObject(obj) {
          return obj === Object(obj);
        }
    

    Usage is something like:

    var output = []

    flatten(input, output);

    Then output should be the flattened array.

    0 讨论(0)
  • 2021-01-05 03:51

    I updated this some more. This is much easier to parse than even console.log because it leaves out the extra stuff that's in there like __proto__.

    function flatten(obj) {
        var empty = true;
        if (obj instanceof Array) {
            str = '[';
            empty = true;
            for (var i=0;i<obj.length;i++) {
                empty = false;
                str += flatten(obj[i])+', ';
            }
            return (empty?str:str.slice(0,-2))+']';
        } else if (obj instanceof Object) {
            str = '{';
            empty = true;
            for (i in obj) {
                empty = false;
                str += i+'->'+flatten(obj[i])+', ';
            }
            return (empty?str:str.slice(0,-2))+'}';
        } else {
            return obj; // not an obj, don't stringify me
        }
    }
    

    The only thing I would do to improve this is have it indent correctly based on recursion level.

    0 讨论(0)
  • 2021-01-05 03:52

    Use the for loop:

    for (var x in yourObj)
    {
        alert(yourObj[x]);
    }
    
    0 讨论(0)
提交回复
热议问题