0

Is there any underscorejs function (or native/custom js function) that transforms this struct :

(please get a look at the real example @Jsfiddle)

//Simple version, check Jsfiddle for the complete one
var object = {
    "users" : [
        {
            name : "name1"
        },
        {
            name : "name2",
        }
    ]
}

to another object like this :

//Simple version, check Jsfiddle for the complete one
var object2 = {
    users : {
        name : "name1, name2"
    }
}
3
  • there isn't one single method for that, but you could combine 2 or 3 that are there to get the job done. Commented Oct 1, 2014 at 17:46
  • I couldn't help but wonder, why in god's name would you want to do that.. ( looking at the fiddle ) Commented Oct 1, 2014 at 18:03
  • 1
    object2={users:name: _.pluck(object.users, "name")}} Commented Oct 1, 2014 at 18:10

5 Answers 5

1

This makes the same object as your fiddle:

function getType(val){
    if(Object.prototype.toString.call(val) == '[object Array]') return 'array';
    if(val !== null && typeof val === 'object') return 'object';
    return 'other';
}

function deepReduce(object){
    var obj={};
    function travel(object, key, obj){
        if( getType(object) == 'array'){ // if its array, travel
            for(var i=0;i<object.length;i++){
                travel(object[i],key,obj)
            }
        }else if(getType(object)=='object'){ // if its object
            for(var i in object){
                 if(getType(object[i])!='other'){ // if property is object, keep travel
                    if(!obj.hasOwnProperty(i)){
                        obj[i]={};  
                    }
                    travel(object[i],i,obj[i])
                }else{ // else, set property
                    obj[i] =  (obj[i] == undefined ? object[i] : obj[i]+',' + object[i]) ;
                }
            }
        }
    }
    travel(object,null, obj);
    return obj;
}


var reduced=deepReduce(object)
console.log(reduced)

Full fiddle: http://jsfiddle.net/97g5v5tt/

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

1 Comment

No problem, took me a bit longer than expected but was worth it. Good luck with your proyect
0

Maybe using reduce? It should be available in ES5.

var users = [{name: 'Joe'}, {name: 'Anna'}];
var names = users.reduce(function(prev, current) {

    return prev.name + ' ' + current.name;
});
console.log(names); // "Joe Anna"

To make this work on your fiddle: http://jsfiddle.net/Lun8kx2k/

You can reduce other keys as well, or write a function that will recursively check keys and reduce them. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Comments

0

You can do it using map function

object2 = {}
object2.name = object.users.map(function(a){
    return a.name;
}).join(",");

2 Comments

Yes but how could i use this in my jsfiddle example ?
if you're returning an array, map() would make more sense than doing extra manip to force reduce() into shape...
0

This uses lodash (and is only partially complete)... Maybe you could try something like this:

var object2 = { users: {} };

_.each(['name', 'gender', 'age'], function (prop) {
  object2.users[prop] = _.pluck(object.users, prop).join(', ');
});

// returns Object { users: { age: "18, 19", gender: "M, F", name: "name1, name2" }}

DEMO

Comments

0

Here is a working solution with a little bit less lines of code:

Object.deepExtend = function xtend(destination, source) {
    for (var property in source) {
        if (source[property] && source[property].constructor && source[property].constructor === Object) {
            destination[property] = destination[property] || {};
            xtend(destination[property], source[property]);
        } else {
            if (!(destination[property] instanceof Array)) {
                destination[property] = destination[property] == null ? source[property] : [destination[property], source[property]].join(",");
            } else {
                destination[property] = destination[property] == null ? source[property] : destination[property].concat(source[property]);
            }
        }
    }
    return destination;
};

Object.deepExtend(object.users[0], object.users[1])

var object = {
    "users" : [
        {
            name : "name1",
            gender : "M",
            age : "18",
            rating : {stars : 5, date : "01.01.2014"},
            articles : [
                {
                    title : "article1",
                    description : "description1"
                },
                {
                    title : "article2",
                    description : "description2"
                }
            ]
        },
        {
            name : "name2",
            gender : "F",
            age : "19",
            rating : {stars : 6, date : "02.01.2014"},
            articles : [
                {
                    title : "article3",
                    description : "description3"
                },
                {
                    title : "article4",
                    description : "description4"
                }
            ]
        }
    ]
}

Object.deepExtend = function xtend(destination, source) {
    for (var property in source) {
        if (source[property] && source[property].constructor && source[property].constructor === Object) {
            destination[property] = destination[property] || {};
            xtend(destination[property], source[property]);
        } else {
            if (!(destination[property] instanceof Array)) {
                destination[property] = destination[property] == null ? source[property] : [destination[property], source[property]].join(",");
            } else {
                destination[property] = destination[property] == null ? source[property] : destination[property].concat(source[property]);
            }
        }
    }
    return destination;
};
document.body.appendChild(document.createElement('pre')).innerHTML = (JSON.stringify(Object.deepExtend(object.users[0], object.users[1]), undefined,3));

1 Comment

But i would like to reduce object to object2 not only the "name" field

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.