3

I need to convert a JavaScript object of one type:

object1: [{"a":"value1", "b":"value2", "c":"value3"}, {"d":"value4","e":"value5","f":"value6"}] 

To another type of object:

object2 : {"value1":["value1", "value2", "value3"], "value4":["value4","value5","value6"]}

I tried to convert it using this function:

function toObject(arr) {
   var rv = {};
   for (var i = 0; i < arr.length; ++i) {
      rv[i] = arr[i];
   }
   return rv;
}

but I'm getting numerical indexes ([0], [1]) instead of "value1" and "value4". Could you please give me some hint how can I do the conversion from object1 to object2. Thanks.

3
  • 1
    There'll be no guaranteed way to determine which key/value pair comes first in each object unless you parse the number out of the "value_" strings, or unless you know that the "a", "b", "c", etc... keys will always be every three letters in the alphabet. Commented Nov 26, 2012 at 20:57
  • 1
    ...but then if your values are as consistent as shown, there's really no reason for the original data structure. You'd only need to know how many Objects in the Array, and how many keys in each Object, so you could just as well use [3,3] to get your resulting structure. Commented Nov 26, 2012 at 21:01
  • I agree with user1689607, if your values are so unique, you may as well just access the values with direct pointers Commented Nov 26, 2012 at 21:04

1 Answer 1

2

what you want is to concatenate the vectors inmates?

Try:

function toObject(arr) {
   var rv = {}, k;
   for (var i = 0; i < arr.length; ++i) {
     for(k in arr[i]){
       rv[k] = arr[i][k];
     }
   }
   return rv;
}

If this is not what you are looking for then try this: [Fixed (with the help of user @user1689607)]

[edit]:

Object.keys does not work in older browsers. [Fixed]

function toObject(arr,_sort) {
    //param1 = Object, param2 = (true:sort, false:default)
    var rv = {}, k, firstV = null, keys, obj, tmp, j,
       ObjK = Object.keys ? function(ke){
            return Object.keys(ke);
       } : function(ke){
            var r = [];
            for(var o in ke){
                r[r.length] = o;
            }
            return r;
       };

    for (var i = 0; i < arr.length; ++i) {
        obj = arr[i];
        tmp = [];
        keys = _sort===true ? ObjK(obj).sort() : ObjK(obj);
        tmp = [obj[keys[0]]];
        for (j = 0; j < keys.length; ++j) {
            tmp[tmp.length] = obj[keys[j]];
        }
        rv[obj[keys[0]]] = tmp;
        firstV = null;
    }
    return rv;
}

//sort
console.log(
    toObject([{"a":"value1", "b":"value2", "c":"value3"}, {"d":"value4","e":"value5","f":"value6"}]),
true);

//default
console.log(
    toObject([{"a":"value1", "b":"value2", "c":"value3"}, {"d":"value4","e":"value5","f":"value6"}])
);
Sign up to request clarification or add additional context in comments.

9 Comments

The trouble with your firstV is that ECMAScript allows an implementation to decide for itself the order of enumeration when using for-in. In other words, there's no guaranteed order, so there's no guarantee that the first item in the enumeration will be the one you expect, e.g. "value1" and "value4".
Thanks. That's the hint I was looking for.
@user1853892: Do you understand that this could randomly fail? The logic is correct except for the issue that I described in my comment above.
@GuilhermeNascimento: Yes, as long as the keys are indeed alphabetical, Object.keys() with .sort() will work. Like this: jsfiddle.net/WFZt9
answer Fixed! Thanks @user1689607 (+1 for you)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.