最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set value to json object from another json object? - Stack Overflow

programmeradmin2浏览0评论

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}
        ];

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}
        ];
Share Improve this question edited Feb 25, 2016 at 7:09 Chris Spittles 15.4k10 gold badges65 silver badges89 bronze badges asked Feb 25, 2016 at 6:56 Suresh BSuresh B 1,6521 gold badge18 silver badges35 bronze badges 2
  • you want manually? check if(a[0].id == "Make"){ b[0].seqNo = a[0]['seqNO']; } – SamiMalik Commented Feb 25, 2016 at 7:04
  • Same type of discussion found at stackoverflow./questions/21450060/… – NeoAsh Commented Feb 25, 2016 at 7:20
Add a ment  | 

7 Answers 7

Reset to default 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/>"));

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.

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 } ]

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

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.

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);

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

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.

发布评论

评论列表(0)

  1. 暂无评论