将嵌套对象展平为一维数组javascript

时间:2017-11-21 00:40:02

标签: javascript jquery

我在这个结构中有一个嵌套对象:

<template>
  <div>
    <p>URL: {{ $g('website_url') }}</p>
    <p>Facebook: {{ fburl }}</p>
    <p><a :href="$g('social.twitter.url')">twitter</a></p>
  </div>
</template>
<script>
export default {
  data () {
    return {
      fburl: this.$g('social.facebook.url')
    }
  }
}
</script>

我试图让它的数据只是一维(Flatten)。我尝试了下面的代码,但它不起作用:

myArray = {
  "D": {
    "U": {
      "A300": "B300",
      "A326": "B326",
      "A344": "B344",
      "A345": "B345"
    },
    "P": {
      "A664": "B664",
      "A756": "B756"
    }
  },
  "I": {
    "U": {
      "A300": "B300",
      "A326": "B326"
    },
    "P": {
      "A756": "B756"
    }
  }
};

var myNewArray = [].concat.apply([], myArray);

我希望var myNewArray = myArray.reduce(function(prev, curr) { return prev.concat(curr); }); 拥有myNewArray

3 个答案:

答案 0 :(得分:2)

您可以这样做:

var myArray = [];
myArray[0] = [];
myArray[0][0] = [];
myArray[0][0][0] = [];
myArray[0][0][1] = [];
myArray[0][1] = [];
myArray[0][1][0] = [];

myArray[0][0][0][0] = "abc1";
myArray[0][0][0][1] = "abc2";
myArray[0][0][1][0] = "abc3";
myArray[0][1][0][1] = "abc4";
myArray[0][1][0][1] = "abc5";


function flat(acc, val){
  if(Array.isArray(val)){
     acc = acc.concat(val.reduce(flat, []));
  }else{
   acc.push(val);
  }
  return acc;
}

var newMyArray = myArray.reduce(flat, []);
console.log(newMyArray);

这样做是递归地减少所有数组的内部值。

看来你正在处理一个物体。您问题的上一个标题和变量名称具有误导性。

在任何情况下,展平对象都是一个非常相似的过程。

var myArray = {"D":{"U":{"A300":"B300","A326":"B326","A344":"B344","A345":"B345"},"P":{"A664":"B664","A756":"B756"}},"I":{"U":{"A300":"B300","A326":"B326"},"P":{"A756":"B756"}}};

function flatObj(obj){
   return Object.keys(obj).reduce(function(acc, key){
     if(typeof obj[key] === "object"){
        acc = acc.concat(flatObj(obj[key]));
     }else{
        acc.push(obj[key]);
     }
     return acc;
   }, []);
}


var newMyArray = flatObj(myArray);
console.log(newMyArray);

答案 1 :(得分:2)

我只想加上我的2美分,因为在我离开工作之前我正在关注这个问题并做出答案。我现在在家,所以我想发布我想出的内容。

const obj = {
  x1: { 
    y1: {
      z1: {
          h1: 'abc',
          h2: 'def'
          },
      z2: {
          h1: 123,
          h2: 456
         }
        }
  }
}

const valAll = getPropValuesAll(obj)
console.log(valAll)

function getPropValuesAll(obj, result = []){
  for(let k in obj){
    if(typeof obj[k] !== 'object'){
    	result.push(obj[k])
        continue
    }
    getPropValuesAll(obj[k], result)
  }
  return result
}

答案 2 :(得分:0)

这将是简单而安全的答案。

var myArray = [["abc1"],[["abc2",,"abc3"]],"abc4",{"r5": "abc5", "r6": "abc6"}];
var myNewArray = [];
function flatten(arr){
  if(Array.isArray(arr)){
    for(var i = 0, l = arr.length; i < l; ++i){
      if(arr[i] !== undefined){
        flatten(arr[i])
      }
    }
  } else if (typeof arr === 'object') {
    for(var key in arr){
      if(arr.hasOwnProperty(key)){
        flatten(arr[key])
      }
    }
  } else {
    myNewArray.push(arr)
  }
}
flatten(myArray)

console.log(myNewArray)