Nodejs da JSON qiymatlarining barcha yoki katta qismini qanday yangilash mumkin?

Ikki json fayl bor - data.json va dataForUpdate.json

data.json:

 [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }]

dataForUpdate.json:

  [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }]

Birinchi jsonni soniyadan boshlab qiymatlari bilan yangilash kerak. Qanday JSON.parse va hardcodded almashtirish holda qilsa bo'ladi.

1
Bu yordam berishi mumkin: JavaScript object correctly clone? stackoverflow.com/questions/728360/…
qo'shib qo'ydi muallif Rajesh, manba
Bu yordam berishi mumkin: JavaScript object correctly clone? stackoverflow.com/questions/728360/…
qo'shib qo'ydi muallif Rajesh, manba
Bu yordam berishi mumkin: JavaScript object correctly clone? stackoverflow.com/questions/728360/…
qo'shib qo'ydi muallif Rajesh, manba
u fayl mazmunini hech qanday ajralishsiz aylantira olmaydi
qo'shib qo'ydi muallif RomanPerekhrest, manba
u fayl mazmunini hech qanday ajralishsiz aylantira olmaydi
qo'shib qo'ydi muallif RomanPerekhrest, manba
u fayl mazmunini hech qanday ajralishsiz aylantira olmaydi
qo'shib qo'ydi muallif RomanPerekhrest, manba
ichidan pastga aylantiriladi
qo'shib qo'ydi muallif Saurabh Agrawal, manba
ichidan pastga aylantiriladi
qo'shib qo'ydi muallif Saurabh Agrawal, manba
ichidan pastga aylantiriladi
qo'shib qo'ydi muallif Saurabh Agrawal, manba

9 javoblar

Object.assign ga qaradingizmi? Siz shunday qila olasiz:

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }]

var dataForUpdate =   [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }]

Object.assign(data[0], dataForUpdate[0]);

Brauzer muvofiqligi

  • Chrome - 45+
  • Firefox - 34+
  • Internet Explorer - not supported
  • Edge - all versions
  • Opera - 32+
  • Safari - 9+
2
qo'shib qo'ydi
Iltimos, brauzer muvofiqligi haqida ogohlantirish qo'shing
qo'shib qo'ydi muallif Rajesh, manba
Faqatgina qiymatlarni yangilash kerak, lekin butun ob'ekt emas. Ushbu usul qiymatlarni almashtiradi, ammo boshqalarni o'chirib tashlaydi.
qo'shib qo'ydi muallif Mykola Svyshch, manba

Object.assign ga qaradingizmi? Siz shunday qila olasiz:

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }]

var dataForUpdate =   [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }]

Object.assign(data[0], dataForUpdate[0]);

Brauzer muvofiqligi

  • Chrome - 45+
  • Firefox - 34+
  • Internet Explorer - not supported
  • Edge - all versions
  • Opera - 32+
  • Safari - 9+
2
qo'shib qo'ydi
Iltimos, brauzer muvofiqligi haqida ogohlantirish qo'shing
qo'shib qo'ydi muallif Rajesh, manba
Faqatgina qiymatlarni yangilash kerak, lekin butun ob'ekt emas. Ushbu usul qiymatlarni almashtiradi, ammo boshqalarni o'chirib tashlaydi.
qo'shib qo'ydi muallif Mykola Svyshch, manba

Object.assign ga qaradingizmi? Siz shunday qila olasiz:

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }]

var dataForUpdate =   [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }]

Object.assign(data[0], dataForUpdate[0]);

Brauzer muvofiqligi

  • Chrome - 45+
  • Firefox - 34+
  • Internet Explorer - not supported
  • Edge - all versions
  • Opera - 32+
  • Safari - 9+
2
qo'shib qo'ydi
Iltimos, brauzer muvofiqligi haqida ogohlantirish qo'shing
qo'shib qo'ydi muallif Rajesh, manba
Faqatgina qiymatlarni yangilash kerak, lekin butun ob'ekt emas. Ushbu usul qiymatlarni almashtiradi, ammo boshqalarni o'chirib tashlaydi.
qo'shib qo'ydi muallif Mykola Svyshch, manba

Har qanday usuldan foydalanmasdan, json orqali yineleyebilir va mos keluvchi kalitlarni yangilashingiz va ob'ektlar uchun o'z-o'zidan qo'ng'iroq qilishingiz mumkin.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = {"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }
 
var dataForUpdate = {"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }
 
 var update = function(a, b) {
  for(key in a) {
    if(b[key] !== undefined) {
      if(typeof b[key] === 'object') {
        update(a[key],b[key]);
      } else {
        a[key] = b[key];
      }
    }
  }
 }
 update(data, dataForUpdate);
 console.log(data);
</div> </div>
1
qo'shib qo'ydi
Javob uchun ko'p rahmat.
qo'shib qo'ydi muallif Mykola Svyshch, manba

Har qanday usuldan foydalanmasdan, json orqali yineleyebilir va mos keluvchi kalitlarni yangilashingiz va ob'ektlar uchun o'z-o'zidan qo'ng'iroq qilishingiz mumkin.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = {"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }
 
var dataForUpdate = {"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }
 
 var update = function(a, b) {
  for(key in a) {
    if(b[key] !== undefined) {
      if(typeof b[key] === 'object') {
        update(a[key],b[key]);
      } else {
        a[key] = b[key];
      }
    }
  }
 }
 update(data, dataForUpdate);
 console.log(data);
</div> </div>
1
qo'shib qo'ydi
Javob uchun ko'p rahmat.
qo'shib qo'ydi muallif Mykola Svyshch, manba

Har qanday usuldan foydalanmasdan, json orqali yineleyebilir va mos keluvchi kalitlarni yangilashingiz va ob'ektlar uchun o'z-o'zidan qo'ng'iroq qilishingiz mumkin.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = {"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }
 
var dataForUpdate = {"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }
 
 var update = function(a, b) {
  for(key in a) {
    if(b[key] !== undefined) {
      if(typeof b[key] === 'object') {
        update(a[key],b[key]);
      } else {
        a[key] = b[key];
      }
    }
  }
 }
 update(data, dataForUpdate);
 console.log(data);
</div> </div>
1
qo'shib qo'ydi
Javob uchun ko'p rahmat.
qo'shib qo'ydi muallif Mykola Svyshch, manba

Agar siz jQuery dan foydalanayotgan bo'lsangiz, Keyin jQuery noreferrer"> kengaytirish dan foydalanishingiz mumkin.

Shunga o'xshash

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }];
 var update = [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }];
 
 $.extend(data[0],update[0]);
 console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
0
qo'shib qo'ydi

Agar siz jQuery dan foydalanayotgan bo'lsangiz, Keyin jQuery noreferrer"> kengaytirish dan foydalanishingiz mumkin.

Shunga o'xshash

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }];
 var update = [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }];
 
 $.extend(data[0],update[0]);
 console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
0
qo'shib qo'ydi

Agar siz jQuery dan foydalanayotgan bo'lsangiz, Keyin jQuery noreferrer"> kengaytirish dan foydalanishingiz mumkin.

Shunga o'xshash

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }];
 var update = [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }];
 
 $.extend(data[0],update[0]);
 console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</div> </div>
0
qo'shib qo'ydi
NodeJS Uzbekistan
NodeJS Uzbekistan
152 ishtirokchilar

Guruh NodeJS bo'yicha muloqot qilish uchun ochilgan. Sizni qiziqtirsa: @nodejs_uz @react_uz @angular_uz @yiiframework_uz @js_uzb @typescript_uzb @vuejs_uz @ngTashkent @laravel_uz @uzdevgroup Ish o'rinlari @UzDev_jobs

Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb