0

I fail miserably at combining all the required transformations. For those more at ease with data manipulation, a little help will be much appreciated, thank you.

Visual representation of what I'm trying to accomplish:

[
    [
        {
            name : 'a',
            value : 'b'
        },
        {
            name : 'c',
            value : 'd'
        },
        ...
    ],
    [
        {
            name : 'e',
            value : 'f'
        },
        {
            name : 'g',
            value : 'h'
        },
        ...
    ],
    ...
]

// to

[
    {
        a : 'b',
        c : 'd'
    },
    {
        e : 'f',
        g : 'h'
    },
    ...
]
4
  • Two things. Pretty sure it's a copy and paste error, but you have b and d as values for both sets of keys. You seem to have lost f and h. Is this a typo or is it the desired behavior? The second note is that what you're showing is not valid JSON. JSON requires that keys and values be quoted in double quotes ", not single quotes '. Commented Jun 11, 2014 at 13:21
  • 4
    Show us how you failed, so that we can help you specifically with your code. Otherwise it would be trivial: return arr.map(function(els){return els.reduce(function(m,el){m[el.name]=el.value;return m;},{});}) - hardly helpful for you. Commented Jun 11, 2014 at 13:25
  • I edited my question regarding the copy paste error. I don't have any code left of my attempts, it was just a big mess really. I'm usually pretty ok at using these data manipulation functions with simple use cases but this one I got confused. How could I train myself to the level where such a problem is "trivial" as you pointed out? Thanks for the solution anyway. Commented Jun 11, 2014 at 13:38
  • Dunno, 24/7 training by answering StackOverflow questions? :-) A clear mental model of what arrays/objects are and how they are structured also helps at identifying the necessary traversing actions. Learning functional programming as well. Commented Jun 11, 2014 at 14:04

3 Answers 3

2

working fiddle here: http://jsfiddle.net/b8YyL/

here's the code:

var original = [
    [
        {
            name : 'a',
            value : 'b'
        },
        {
            name : 'c',
            value : 'd'
        },
    ],
    [
        {
            name : 'e',
            value : 'f'
        },
        {
            name : 'g',
            value : 'h'
        },
    ],
]


function translateNameValueObjects(items) {
    var map = {},
    item,
    i;
    for (i = 0; i < items.length; i++) {
        item = items[i];
        map[item.name] = item.value;
    }
    return map;
}

var mapped = original.map(translateNameValueObjects);
alert(JSON.stringify(mapped));
Sign up to request clarification or add additional context in comments.

Comments

0

Please check the below code. hop this help you!

var jsonArray = [
    [
        {
            name : 'a',
            value : 'b'
        },
        {
            name : 'c',
            value : 'd'
        },
    ],
    [
        {
            name : 'e',
            value : 'f'
        },
        {
            name : 'g',
            value : 'h'
        },
    ]
];

var modifiedJsonArray = [];        
if(jsonArray.length){
    for(var i=0; i<jsonArray.length; i++){
        if(jsonArray[i].length){
            for(var j=0; j<jsonArray[i].length; j++){
                modifiedJsonArray.push("{"+jsonArray[i][j].name+":"+jsonArray[i][j].value+"}")
            }
        }
    }
}

console.log(modifiedJsonArray);

JSFiddle: http://jsfiddle.net/mail2asik/f7euL/

3 Comments

Those if-tests are absolutely superfluous.
@Bergi- The above answer is useful. than mark a answer is useful.
I think long-winded code is not useful. Please let me vote with my own opion.
0

You can use reduce function:

var data = [
 [
    {
        name : 'a',
        value : 'b'
    },
    {
        name : 'c',
        value : 'd'
    },
 ],
 [
    {
        name : 'e',
        value : 'f'
    },
    {
        name : 'g',
        value : 'h'
    }
 ]
];

data = data.reduce(function(a, b, i){
    a[i] = b.reduce(function(c, d){
        c[d.name] = d.value;
        return c;
    }, {});
    return a;
}, []);

JSFiddle http://jsfiddle.net/7whFy/1/

Comments

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.