Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to transform something like
var a = [{id:1, prop1:"value1", prop2:"value2"},{id:2, prop1:"value1", prop2:"value2"}] 

into
var a = [{id:1, wrap: {prop1:"value1", prop2:"value2"}},{id:2, warp:{ prop1:"value1", prop2:"value2"}}]


like keeping the id, moving/ wrapping the rest in a new property.

I thought this could be easily done (without cloning, deleting, iterating) using underscore, and ideas!?

Thanks.
Posted
Comments
Sergey Alexandrovich Kryukov 28-Aug-14 15:39pm    
What do you mean by "transfer"? Why not having the object in a different structure in first place?
The simple answer would be: have two objects in both structures, and then populate one object with properties of another one. But is it what you want. To get an answer, you would need to clarify this otherwise a pretty simple problem.
—SA
Oliver Bleckmann 28-Aug-14 17:02pm    
What do you mean by "transfer"?

See the example! As it is valid JavaScript, it is as clear as it could be (in a mathematically sense)!

Oh, OK, got it. This could be misleading. I am not talking about parsing. It's about object transformation like there dozens of samples at the underscore website e.g.

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.pluck(stooges, 'name');
=> ["moe", "larry", "curly"]
Sergey Alexandrovich Kryukov 28-Aug-14 23:57pm    
I did not receive notification, because you added a comment to your own post and not to my comment. Next time, please take it into account and use Reply. The authors of direct or indirect parent comments will receive notifications.

I am not talking about parsing, too. It looks like you really need to move data from one structure to another. You can create another variable and add properties and data one by one. In both cases, you have array of structures with two elements, so the keys to associative containers are 0 and 1. Element types are different: object with two properties or three properties. It means that three properties should be added one by one, with added "wrap". Looks pretty simple...

Do you understand the basic fact: all structured objects are associative containers, and array is the same as structure with N properties, and property is the key to an array?

—SA

1 solution

C#
//used beause omit didn't work

_.mixin({
    remove: function(obj, key){
        delete obj[key];
        return obj;
    }
});

var a = [{id:1, prop1:"value1", prop2:"value2"},{id:2, prop1:"value1", prop2:"value2"}];
//transform
a = _.map(a, function(value) {
  return { id:value.id ,rows: _.remove(value, "id") };
});
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900