3

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}
        ];
Chris Spittles
  • 15,023
  • 10
  • 61
  • 85
Suresh B
  • 1,658
  • 1
  • 17
  • 35
  • you want manually? check if(a[0].id == "Make"){ b[0].seqNo = a[0]['seqNO']; } – SamiMalik Feb 25 '16 at 07:04
  • Same type of discussion found at http://stackoverflow.com/questions/21450060/how-to-join-two-json-object-in-javascript-without-using-jquery – NeoAsh Feb 25 '16 at 07:20

7 Answers7

1

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.

ChrisY
  • 1,681
  • 10
  • 12
1

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/>"));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

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.

0

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 } ]
Taj Ahmed
  • 895
  • 11
  • 19
0

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);
haotang
  • 5,520
  • 35
  • 46
0

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...

Mohideen Asraf
  • 32
  • 1
  • 1
  • 8
-1

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.