How to convert Object to array with custom format

时间:2018-03-25 18:55:43

标签: javascript arrays node.js

This is my javascript Object

{
  username: 'hamet',
  weight: 75,
  active: false,
  data: {
     interestedIn: ['internet', 'ping pong'],
     anotherArr: [{data: [1,2,3] }],
     anotherObj: {
       // and another obj or array or any type of field
     }
  }
}

i need to convert this Object to array with following format

let arr = [
'username', 'hamet', 'username_type', 'string',
'weight', '75', 'weight_type', 'number',
'active', 'true', 'active_type', 'boolean',

'data.interestedIn', 'array', 'data.interestedIn_type', 'array',
'data.interestedIn.0', 'internet', 'data.interestedIn.0_type','string',
'data.interestedIn.1', 'ping pong', 'data.interestedIn.1_type','string',

'data.anotherArr', 'array', 'data.anotherArr_type', 'array',
'data.anotherArr.0.data', 'array', 'data.anotherArr.0.data_type','array',
'data.anotherArr.0.data.0', '1', 'data.anotherArr.0.data.0_type','number',
// ...
]

i need this type of array to save data in redis with hmset command, also i won't use JSON.stringify for whole document

the bad news is, none of the fields has constant value with constant data type, maybe there is a object into object into object..

2 个答案:

答案 0 :(得分:2)

You could use an iterative and recursive approach by iterating the properties and collecting nested properties and values.

function getKeysValues(object) {

    function iter(o, p) {
        Object.keys(o).forEach(function (k) {
            var path = (p.concat(k)).join('.'),
                type = Array.isArray(o[k]) &&  'array' || typeof o[k];;
            if (o[k] && typeof o[k] === 'object') {
                result.push(path, type, path + '_type', type);
                return iter(o[k], p.concat(k));
            }
            result.push(path, o[k], path + '_type', type);
        });
    }

    var result = [];
    iter(object, [])
    return result;
}

var object = { username: 'hamet', weight: 75, active: false, data: { interestedIn: ['internet', 'ping pong'], anotherArr: [{ data: [1, 2, 3] }], anotherObj: { foo: 'bar' } } };

console.log(getKeysValues(object));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Without object as type

function getKeysValues(object) {

    function iter(o, p) {
        Object.keys(o).forEach(function (k) {
            var path = (p.concat(k)).join('.'),
                type = Array.isArray(o[k]) &&  'array' || typeof o[k];

            if (o[k] && typeof o[k] === 'object') {
                if (type === 'array') {
                    result.push(path, type, path + '_type', type);
                }
                return iter(o[k], p.concat(k));
            }
            result.push(path, o[k], path + '_type', type);
        });
    }

    var result = [];
    iter(object, [])
    return result;
}

var object = { username: 'hamet', weight: 75, active: false, data: { interestedIn: ['internet', 'ping pong'], anotherArr: [{ data: [1, 2, 3] }], anotherObj: { foo: 'bar' } } };

console.log(getKeysValues(object));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

You can use Object.keys() to get a list of all the keys in your object. Then, combine it with typeof, to figure out the type of the value.

Something like

var output = [];
Object.keys(yourObject).forEach(function(key) {
  output.push(key);
  output.push(yourObject[key]);
  output.push(key + '_type');
  output.push(typeof yourObject[key]);
});

You need to do something different when you encounter an array as a value. I'll leave that up to you.