Suresh B
Suresh B

Reputation: 1652

How to set value to json object from another json object?

I have two json objects: a, b. I need to set seqNo to b json object from a json object based on the id. How would I go about doing this?

var a = [{id: "Make",   seqNo: 4},
            {id: "Model",  seqNo: 1},
            {id: "XModel", seqNo: 2},
            {id: "Rate",   seqNo: 3},
            {id: "Price",  seqNo: 0}];

var b = [
           {id: "Make", field: "make", seqNo: setvalue},
           {id: "Model", field: "model", seqNo: setvalue},
           {id: "XModel", field: "model", seqNo: setvalue},
           {id: "Rate", field: "price", seqNo: setvalue},
           {id: "Price", field: "price", seqNo: setvalue}
        ];

output:-

var c = [
            {headerName: "Make", field: "make", seqNo: 4},
            {headerName: "Model", field: "model", seqNo: 1},
            {headerName: "XModel", field: "model", seqNo: 2},
            {headerName: "Rate", field: "price", seqNo: 3},
            {headerName: "Price", field: "price", seqNo: 0}
        ];

Upvotes: 3

Views: 8751

Answers (7)

Mohideen Asraf
Mohideen Asraf

Reputation: 32

Here is Your code :

var a = [{id: "Make",   seqNo: 4},
        {id: "Model",  seqNo: 1},
        {id: "XModel", seqNo: 2},
        {id: "Rate",   seqNo: 3},
        {id: "Price",  seqNo: 0}];

// make it null of seqNo
var b = [
       {id: "Make", field: "make", seqNo: null},
       {id: "Model", field: "model", seqNo: null},
       {id: "XModel", field: "model", seqNo: null},
       {id: "Rate", field: "price", seqNo: null},
       {id: "Price", field: "price", seqNo: null}
    ];

Set Value here

Just Make it :

   if(a.length == b.length){
      for(var i=0; i<a.length;i++){
         b[i].seqNo = a[i].seqNo;
      }
    }
    else{ console.log('Error','length not Match')}

Also Alternate Way :

You can assign directly if you have a two object:

var b = [
       {id: "Make", field: "make", seqNo: a[0].seqNo},
       {id: "Model", field: "model", seqNo: a[1].seqNo},
       {id: "XModel", field: "model", seqNo: a[2].seqNo},
       {id: "Rate", field: "price", seqNo: a[3].seqNo},
       {id: "Price", field: "price", seqNo: a[4].seqNo}
    ]; 

Hope this help for you...

Upvotes: 0

haotang
haotang

Reputation: 5718

Thanks for this challenging question.

This is my solution using lodash:

var c = _.zipWith(b, a, function(obj1, obj2) {
    var object= _.assign({}, obj1, obj2);
    object.headerName = object.id; 
    delete object.id; 
    return object;
});

console.log(c);

Upvotes: 0

ChrisY
ChrisY

Reputation: 1783

I would try something like this:

var mapped_a = a.reduce(function(result, element) {
    result[element.id] = element.seqNo;
    return result;
}, {});

// mapped_a => {Make: 4, Model: 1,.. }

var mapped_b = b.map(function(element) {
    var newElement = {
        id: element.id,
        field: element.field,
        seqNo: mapped_a[element.id]
    };
    return newElement;
});

In the first step I convert the array to an object map. Then I can access the correct element via the array syntax in the map function.

Upvotes: 1

Alexander Bychkov
Alexander Bychkov

Reputation: 78

If you're using underscorejs you should use the following code for merge two objects

_.extend({id: "Make", field: "make", seqNo: setvalue}, {id: "Make", seqNo: 4})

If you're using Jquery then this code

$.extend({id: "Make", field: "make", seqNo: setvalue}, {id: "Make", seqNo: 4})

If you need a plain javascript function you can find it at: http://youmightnotneedjquery.com/

So, you have two arrays of objects, so you need use map or another method to go through your array and apply for each element the extending function.

Upvotes: 0

Taj Ahmed
Taj Ahmed

Reputation: 895

here is the looping solution assuming you have equal number of objects in both array to get the satisfying result

var a = [{id: "Make",   seqNo: 4},
            {id: "Model",  seqNo: 1},
            {id: "XModel", seqNo: 2},
            {id: "Rate",   seqNo: 3},
            {id: "Price",  seqNo: 0}];

var b = [
           {id: "Make", field: "make", seqNo: 'setvalue'},
           {id: "Model", field: "model", seqNo: 'setvalue'},
           {id: "XModel", field: "model", seqNo: 'setvalue'},
           {id: "Rate", field: "price", seqNo: 'setvalue'},
           {id: "Price", field: "price", seqNo: 'setvalue'}
        ];

b = b.map(function (obj) {
    a.forEach(function (aObj,aIndex) {
        if (obj.id == a[aIndex].id) {
            obj["seqNo"] = a[aIndex].seqNo;
        }
    })
    return obj;
})
console.log(b)

// Sample result
[ { id: 'Make', field: 'make', seqNo: 4 },
  { id: 'Model', field: 'model', seqNo: 1 },
  { id: 'XModel', field: 'model', seqNo: 2 },
  { id: 'Rate', field: 'price', seqNo: 3 },
  { id: 'Price', field: 'price', seqNo: 0 } ]

Upvotes: 0

Ebed Kharistian
Ebed Kharistian

Reputation: 82

I suggest using for loop in javascript.

var a = [{id: "Make",   seqNo: 4},
        {id: "Model",  seqNo: 1},
        {id: "XModel", seqNo: 2},
        {id: "Rate",   seqNo: 3},
        {id: "Price",  seqNo: 0}];
var b = [
       {id: "Make", field: "make", seqNo: 0},
       {id: "Model", field: "model", seqNo: 0},
       {id: "XModel", field: "model", seqNo: 0},
       {id: "Rate", field: "price", seqNo: 0},
       {id: "Price", field: "price", seqNo: 0 }
    ];
for (var iB in b) {
   for(var iA in a){

   var objB = b[iB];
   var objA = a[iA];
        if(objB.id == objA.id ){
          objB.seqNo = objA.seqNo;
        }
    }
}
console.log(b)

JSfiddle Link

Hope it helps.

Upvotes: -1

mplungjan
mplungjan

Reputation: 178126

Assuming same order:

var setvalue="";
var a = [{id: "Make",   seqNo: 4},
         {id: "Model",  seqNo: 1},
         {id: "XModel", seqNo: 2},
         {id: "Rate",   seqNo: 3},
         {id: "Price",  seqNo: 0}];

var b = [{id: "Make", field: "make", seqNo: setvalue},
         {id: "Model", field: "model", seqNo: setvalue},
         {id: "XModel", field: "model", seqNo: setvalue},
         {id: "Rate", field: "price", seqNo: setvalue},
         {id: "Price", field: "price", seqNo: setvalue}
        ];

var c=[];
for (var i=0;i<a.length;i++) {
  var aItem = a[i], bItem=b[i];
  c.push({ headerName:aItem.id, field:bItem.field, seqNo: aItem.seqNo });
}
console.log(c);
document.write(JSON.stringify(c, null, 2).replace(/},/g,"},<br/>"));

Upvotes: 1

Related Questions