有趣数据结构转化,如何实现?


原始数据


 javascript


 var obj = {
    'a': 1,
    'b': 2,
    'c': ['x','y','z','j'],
    'd': ['a','c','u'],
    'e': ['e', 'd']
};

转化为目标数据


 javascript


 [
    {'a': 1, 'b': 2, 'c': 'x', 'd': 'a', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'y', 'd': 'a', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'z', 'd': 'a', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'j', 'd': 'a', 'e': 'e'},

    {'a': 1, 'b': 2, 'c': 'x', 'd': 'c', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'y', 'd': 'c', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'z', 'd': 'c', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'j', 'd': 'c', 'e': 'e'},

    {'a': 1, 'b': 2, 'c': 'x', 'd': 'u', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'y', 'd': 'u', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'z', 'd': 'u', 'e': 'e'},
    {'a': 1, 'b': 2, 'c': 'j', 'd': 'u', 'e': 'e'},

    {'a': 1, 'b': 2, 'c': 'x', 'd': 'a', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'y', 'd': 'a', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'z', 'd': 'a', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'j', 'd': 'a', 'e': 'd'},

    {'a': 1, 'b': 2, 'c': 'x', 'd': 'c', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'y', 'd': 'c', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'z', 'd': 'c', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'j', 'd': 'c', 'e': 'd'},

    {'a': 1, 'b': 2, 'c': 'x', 'd': 'u', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'y', 'd': 'u', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'z', 'd': 'u', 'e': 'd'},
    {'a': 1, 'b': 2, 'c': 'j', 'd': 'u', 'e': 'd'},
]

参数是不定的,


 var obj1 = {
    'a': 1,
    'b': 2,
    'c': ['x','y','z','j'],
    'd': ['a','c','u'],
    'e': ['e', 'd']
}

var obj2 = {
    'a': 1,
    'b': 2,
    'c': ['x','y','z','j'],
    'd': ['a','c','u']
}

var obj3 = {
    'c': ['x','y','z','j'],
    'd': ['a','c','u']
}

var obj4 = {
    'a': 1,
    'b': 2
}

等等,对象个数没有限制,属性值没有限制(当然,只翻译属性值为数组类型的)

javascript对象 递归 数据结构 JavaScript javascript数组

酱酱酱酱。 10 years, 2 months ago

话说这不是sku么

衔尾蛇与五月柱 answered 10 years, 2 months ago

需要按照你那种顺序输出么,如果不需要可能更简单,随便怎样遍历一下这个树都可以.

如果需要按照你写的那种顺序,有两个方案:
1、广度优先搜索一下,不过是倒过来搜索,从最后面的元素开始。
2、随便怎样便利一下,然后对结果进行排序。

不过话说回来,对象内部的属性顺序本来就没有意义啊

潇洒的一逼 answered 10 years, 2 months ago


 javascript


 function transform(obj, keys, index, result) {
  var key = keys[index];
  var value = obj[key];
  if (Array.isArray(value)) {
    var newObj = {};
    var newObj2 = {};
    keys.forEach(function (k) {
      newObj[k] = k === key ? value[0] : obj[k];
      newObj2[k] = k === key ? value.slice(1) : obj[k];
    });
    if (newObj[key] !== undefined) {
      transform(newObj, keys, index, result);
    }
    if (newObj2[key].length > 0) {
      transform(newObj2, keys, index, result);
    }
  } else {
    if (index === keys.length - 1) {
      var ret = {};
      keys.forEach(function (k) {
        ret[k] = obj[k];
      });
      result.push(ret);
    } else {
      transform(obj, keys, index + 1, result);
    }
  }
}

var obj = {
  'a': 1,
  'b': 2,
  'c': ['x','y','z','j'],
  'd': ['a','c','u'],
  'e': ['e', 'd']
};
var result = [];
transform(obj, Object.keys(obj), 0, result);

result.forEach(function (o) {
  console.log('%j', o);
});

奔跑的萝卜 answered 10 years, 2 months ago


 js


 var oTransformer = function ( obj ) {
    this.data = obj;
    this.dataArr = [];
    this.resultArr = [];
    this.result = [];
};

oTransformer.prototype.parseSrc = function () {
    var objMapping = [],
        isArray = function(obj) {
            return Object.prototype.toString.call(obj) === '[object Array]';
        };

    for (var key in this.data) {
        var value = this.data[key],
            arr = [];

        if (!isArray(value)) {
            value = [ value ];
        }

        for (var i = 0; i < value.length; i++) {
            arr.push(key + ':' + value[i]);
        }

        this.dataArr.push(arr);
    }

    console.log(this.dataArr);
};

oTransformer.prototype.transform = function () {
    var point  = {},
        pIndex = null,
        tempCount = 0,
        temp = [],
        list = this.dataArr;

    for (var index in list) {
        if (typeof list[index] == 'object') {
            point[index] = { 'parent': pIndex, 'count': 0 };
            pIndex = index;
        }
    }

    if (pIndex == null) {
        this.resultArr =  list;
        return;
    }

    while(true) {
        for (var index in list) {
            tempCount = point[index]['count'];
            temp.push(list[index][tempCount]);
        }

        this.resultArr.push(temp);
        temp = [];

        while(true) {
            if (point[index]['count'] + 1 >= list[index].length) {
                point[index]['count'] = 0;
                pIndex = point[index]['parent'];
                if (pIndex == null) {
                    return;
                }

                index = pIndex;
            } else {
                point[index]['count']++;
                break;
            }
        }
    }
};

oTransformer.prototype.toObjArr = function () {
     for (var i = 0; i < this.resultArr.length; i++) {
        var line = this.resultArr[i],
            obj = {};

        for (var j = 0; j < line.length; j++) {
            var cur = line[j],
                key = cur.split(':')[0],
                value = cur.split(':')[1];
            obj[key] = value;
        }

        this.result.push(obj);
    }
};

oTransformer.prototype.process = function () {
    this.parseSrc();
    this.transform();
    this.toObjArr();
};

var obj = {
    'a': 1,
    'b': 2,
    'c': ['x','y','z','j'],
    'd': ['a','c','u'],
    'e': ['e', 'd']
};

var t = new oTransformer(obj);
t.process();

console.log(t.result);

畢竟東流去 answered 10 years, 2 months ago

Your Answer