0

Object 1:

{a50: "PTP-1",b51: "09+75",c52: "112D",d53: "60.745",e54: "72.698",f55: "72.695",g56: "0.003",h57: null,i58: null, j59: "68.918", k60: null}

object 2:

[{ipid:'50', type:'Constant'},{ipid:'51', type:'Constant'},{ipid:'52', type:'Constant'},{ipid:'53', type:'Constant'},{ipid:'54', type:'Constant'},{ipid:'55', type:'Constant'},{ipid:'56', type:'Constant'},{ipid:'57', type:'Variable'},{ipid:'58', type:'Variable'},{ipid:'59', type:'Variable'},{ipid:'60', type:'Variable'}]

(Screenshot)

I have this two javascript object which have the same length and i would like to combine and produce a new javascript object

[
    {ipid: "50", type: "Constant", a50: "PTP-1"},
    {ipid: "51", type: "Constant", b51: "09+75"},
    {ipid: "52", type: "Constant", c52: "112D"},
    {ipid: "53", type: "Constant", d53: "60.745"},
    {ipid: "54", type: "Constant", e54: "72.698"},
    {ipid: "55", type: "Constant", f55: "72.695"},
    {ipid: "56", type: "Constant", g56: "0.003"},
    {ipid: "57", type: "Variable", h57: null},
    {ipid: "58", type: "Variable", i58: null},
    {ipid: "59", type: "Constant", j59: "68.918"},
    {ipid: "60", type: "Variable", k60: null}
]

am looking for faster and efficient code. (Perhaps just in native js)

Thank you

3
  • Can you provide code of the second object, instead of a screenshot? Also, what's with the single-character prefix in object 1? If it didn't have that, this would be so much simpler and more efficient. Commented May 20, 2014 at 9:35
  • Can you show us what you tried so far? And a demo to reproduce your particular issue where you got stuck? Commented May 20, 2014 at 9:40
  • As far as I can see from the screenshot your object 2 is a Array.. I have tried what I think you want to do: jsfiddle.net/2SM3W Commented May 20, 2014 at 9:45

2 Answers 2

1

Have a look at this:

var obj1 = {a50: "PTP-1",b51: "09+75",c52: "112D",d53: "60.745",e54: "72.698",f55: "72.695",g56: "0.003",h57: null,i58: null, j59: "68.918", k60: null}
var obj2 = [{ipid:'50', type:'Constant'},{ipid:'51', type:'Constant'},{ipid:'52', type:'Constant'},{ipid:'53', type:'Constant'},{ipid:'54', type:'Constant'},{ipid:'55', type:'Constant'},{ipid:'56', type:'Constant'},{ipid:'57', type:'Variable'},{ipid:'58', type:'Variable'},{ipid:'59', type:'Variable'},{ipid:'60', type:'Variable'}]

var output = [],
    key = '';

for(key in obj1){
    if(obj1.hasOwnProperty(key)){
        var current = obj2.filter(function(e){
            return e.ipid == key.substr(1)
        });
        for(var i = 0; i < current.length; i++){
            var temp = {
                ipid: current[i].ipid,
                type: current[i].type
            }
            temp[key] = obj1[key];
            output.push(temp);
        }
    }
}

This returns the following result:

[
    {"ipid": "50", "type": "Constant", "a50": "PTP-1"},
    {"ipid": "51", "type": "Constant", "b51": "09+75"},
    {"ipid": "52", "type": "Constant", "c52": "112D"},
    {"ipid": "53", "type": "Constant", "d53": "60.745"},
    {"ipid": "54", "type": "Constant", "e54": "72.698"},
    {"ipid": "55", "type": "Constant", "f55": "72.695"},
    {"ipid": "56", "type": "Constant", "g56": "0.003"},
    {"ipid": "57", "type": "Variable", "h57": null},
    {"ipid": "58", "type": "Variable", "i58": null},
    {"ipid": "59", "type": "Variable", "j59": "68.918"},
    {"ipid": "60", "type": "Variable", "k60": null}
]
Sign up to request clarification or add additional context in comments.

2 Comments

i use use strict version. Any thank you so much for your help for saving the day
@MuhaiminAbdul: In that case, the key is a string ;-) Thanks for accepting my answer.
1
var a = {a50: "PTP-1",b51: "09+75",c52: "112D",d53: "60.745",e54: "72.698",f55: "72.695",g56: "0.003",h57: null,i58: null, j59: "68.918", k60: null};
var b = [{ipid:'50', type:'Constant'},{ipid:'51', type:'Constant'},{ipid:'52', type:'Constant'},{ipid:'53', type:'Constant'},{ipid:'54', type:'Constant'},{ipid:'55', type:'Constant'},{ipid:'56', type:'Constant'},{ipid:'57', type:'Variable'},{ipid:'58', type:'Variable'},{ipid:'59', type:'Variable'},{ipid:'60', type:'Variable'}];

var aTransformed = {};

for(var key in a) {
    var obj = {};
    obj[key] = a[key];
    aTransformed[key.slice(1)] = { key: a[key] };
}

b = _.map(b, function (obj) {
    return _.extend(obj, aTransformed[obj.ipid]);
});

1 Comment

thanks @Taner. _.map n _.extend indeed an alternative. But I prefer above answer because it's vanillajs

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.