Boshqa xaritaga ko'chiring va tugunni solishtiring

Men JavaScript-ni ob'ekti va JavaScript-da bir qator vazifalarga aylantirish haqida ba'zi shubhalar bilan. Aytaylik, bu o'zgaruvchilari bor:

var json1 = "[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]";
var json2 = "[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]";

Bir qatorda faqat identifikatorlar bilan o'zgaruvchini qanday qilib o'zgartirishim mumkin

var ids1 = json1.ids (would be 1,2)
var ids2 = json2.ids (would be 1,2,3)

va faqat boshqa identifikatorlar bilan boshqa o'zgaruvchini tanlang

var idsdiff = diff(ids1, ids2) (would be 3)
4
Sizning JavaScript jildingiz noto'g'ri. Sizda tirnoqlarni kiritishingiz mumkin - shuningdek, JSON JavaScript emas Ob'ektni
qo'shib qo'ydi muallif mplungjan, manba

6 javoblar

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

var json1 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}],
    json2 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}, {"id":3,"name":"z"}],
    result1 = json1.map(function (a) { return a.id; }),
    result2 = json2.map(function (a) { return a.id; });

var diffs = result2.filter(function (item) {    
    return result1.indexOf(item) < 0;
});

console.log(result1);
console.log(result2);
console.log(diffs);
</div> </div>

Eslatma indexOf va iE9 oldin iE da map va map mavjud emas.

UPDATE: as per @alexandru-Ionutmihai's comment, filter will fail on [1,2,4] and [1,2,3]

Ushbu kod yaxshi ko'rinadi:

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

var json1 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}],
        json2 = [{"id":1,"name":"x"}, {"id":2,"name":"y"}, {"id":3,"name":"z"}],
        result1 = json1.map(function (a) { return a.id; }),
        result2 = json2.map(function (a) { return a.id; });

//as per @alexandru-Ionutmihai this is inaccurate for [1,2,4] and [1,2,3]
/*var diffs = result2.filter(function (item) {  
    return result1.indexOf(item) < 0;
});*/

//here's a workaround
function arr_diff(a, b) {
  var i,
    la = a.length,
    lb = b.length,
    res = [];
  if (!la)
    return b;
  else if (!lb)
    return a;
  for (i = 0; i < la; i++) {
    if (b.indexOf(a[i]) === -1)
      res.push(a[i]);
  }
  for (i = 0; i < lb; i++) {
    if (a.indexOf(b[i]) === -1) res.push(b[i]);
  }
  return res;
}

var diffs = arr_diff(result1, result2),
  testDiff = arr_diff([1, 2, 4], [1, 2, 3]);

console.log(result1);
console.log(result2);
console.log(diffs);
console.log(testDiff);
</div> </div>

arr_diff credit to @Nomaed's comment on this question's answer.

2
qo'shib qo'ydi
tahrirlangan javob ..
qo'shib qo'ydi muallif Geoman Yabes, manba
json1 = [1,2,3] , json2 = [1,2,4] kabi qatorlardan foydalanganda @ alexandru-Ionutmihai aytganda, bu usul ishlamaydi
qo'shib qo'ydi muallif ElChiniNet, manba

id uchun hash jadvali foydalanishingiz mumkin va qiymatni farq bilan belgilashingiz mumkin. Keyin natijani filtrlash orqali bajaring.

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

function getId(a) { return a.id; }

var obj1 = JSON.parse('[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}]');
var obj2 = JSON.parse('[{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}]');
var ids1 = obj1.map(getId);
var ids2 = obj2.map(getId);
var hash = {};

ids1.forEach(function (a) {
    hash[a] = 1;
});
ids2.forEach(function (a) {
    hash[a] = (hash[a] || 0) - 1;
});

var difference = Object.keys(hash).filter(function (a) { return hash[a]; }).map(Number);
console.log(ids1);
console.log(ids2);
console.log(hash);
console.log(difference);
.as-console-wrapper { max-height: 100% !important; top: 0; }
</div> </div>

Sizning fikringiz uchun _. Chor dan foydalanishingiz mumkin. nosimmetrik farq.

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

var ids1 = [1, 2],
    ids2 = [1, 2, 3];

console.log(_.xor(ids1, ids2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
</div> </div>
2
qo'shib qo'ydi

xarita </​​code> usulini filter usuli bilan birgalikda ishlatishingiz mumkin.

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

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}];
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}];
var j1=json1.map((x)=>{return x.id});
var j2=json2.map((x)=>{return x.id});
var diff = j2.filter(function(el){
    return j1.indexOf(el)==-1;
}).concat(j1.filter(function(el){
    return j2.indexOf(el)==-1;
}));
console.log(diff);
</div> </div>

Bundan tashqari, ushbu kod har ikkala json majmuasi boshqa IDs bo'lsa, ishlaydi.

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

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 4, "name":"y"}, {"id": 5, "name":"y"}];
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}];
var j1=json1.map((x)=>{return x.id});
var j2=json2.map((x)=>{return x.id});
var diff = j2.filter(function(el){
    return j1.indexOf(el)==-1;
}).concat(j1.filter(function(el){
    return j2.indexOf(el)==-1;
}));
console.log(diff);
</div> </div>
1
qo'shib qo'ydi
chiroyli yechim, juda toza. rahmat.
qo'shib qo'ydi muallif 2Fast4YouBR, manba

Agar JSONs bu ayrıştırılmamışsa, avval qo'shimcha bir qadam kerak:

json1 = JSON.parse(json1);

Aks holda, iltimos, ushbu kodni kiriting:

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

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}];
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}];

// extra steps, if necessary
// json1 = JSON.parse(json1);
// json2 = JSON.parse(json2);

function returnID (item) {
    return item.id;
};

json1 = json1.map(returnID);
json2 = json2.map(returnID);

var diff = json2.filter(function (item) {
    return json1.indexOf(item) < 0;
});

console.log(diff);
</div> </div>
1
qo'shib qo'ydi
Agar kod va json2 = [1,2,3] bo'lsa, ushbu kod ishlamaydi.
qo'shib qo'ydi muallif Mihai Alexandru-Ionut, manba

Har bir ob'ektning faqat id xususiyatlariga ega bo'lgan qatorlarni olish uchun, oddiy ...

var ids1 = json1.map(x => x.id)
var ids2 = json2.map(x => x.id)

Agar siz ES6 yoki versiya transpileridan foydalanayotgan bo'lsangiz, quyidagi ikkita orasidagi farqni olish uchun spred operatoridan foydalanishingiz mumkin:

var diff = [...id1.filter(x => id2.indexOf(x) == -1), ...id2.filter(x => id1.indexOf(x) == -1)]

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

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}];
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}];

var ids1 = json1.map(x => x.id);
var ids2 = json2.map(x => x.id);

var diff = [...ids1.filter(x => ids2.indexOf(x) == -1), ...ids2.filter(x => ids1.indexOf(x) == -1)];
console.log(diff);
</div> </div>
0
qo'shib qo'ydi

Bu erda sizga kerakli natijalarni olish uchun men sizga ikkita vazifani qo'yaman:

Birinchi funktsiya (getIds):

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

var json1 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}];
var json2 = [{"id": 1, "name":"x"}, {"id": 2, "name":"y"}, {"id": 3, "name":"z"}];

function getIds (array) {
    return array.map(function (obj) {
        return obj.id;
    });
}

console.log(getIds(json1));
console.log(getIds(json2));
</div> </div>

Ikkinchi funksiya (getDiff)

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

var json1 = [1, 2, 4, 5];
var json2 = [1, 2, 3];

function getDiff (array1, array2) {
    return array1.concat(array2).filter(function (id, index, arr) {
        return arr.indexOf(id) === arr.lastIndexOf(id);
    });
}

console.log(getDiff(json1, json2));
</div> </div>
0
qo'shib qo'ydi
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