javascript obyektlarini o'z ichiga olgan ko'p o'lchovli qatorni qanday tekislash kerak

Ob'ektdagi ob'ektlar bilan ko'p o'lchovli qator bor ... Qanday qilib uni tekislashim mumkin?

myarr[0] =[{"name":"john","age":"50","location":"san diego"}
           ,{"name":"jane","age":"25","location":"new york"}
           ,{"name":"susane","age":"10","location":"los angeles"}     
               ];
myarr[1] =[{"smoker":"yes","drinker":"no","insured":"no"}
           ,{"smoker":"no","drinker":"no","insured":"yes"}
           ,{"smoker":"no","drinker":"yes","insured":"no"}     
               ];
myarr[1] =[{"status":"married","children":"none"}
           ,{"status":"unmarried","children":"one"}
           ,{"status":"unmarried","children":"two"}     
               ];
1
concat ni tekshirib ko'ring bu erda .
qo'shib qo'ydi muallif Matt Fenwick, manba
tekislash - ko'p o'lchovli qatorni tekislashni anglatadi - uning bir o'lchamli majmuasini ishlab chiqing - demak, chalkashlik uchun uzr so'rayman. Masalan, barcha buyumlar o'z ichiga olgan tartibda ortib boradi.
qo'shib qo'ydi muallif learner, manba

1 javoblar

O'ylaymanki, buni qilishni xohlaysan.

Avvalo oddiy yordamchi funktsiyani ikkita moslamani birlashtirishni xohlaysiz:

function merge(a, b) {
    a = a || { };
    for(var k in b)
        if(b.hasOwnProperty(k))
            a[k] = b[k];
    return a;
}

Shundan so'ng siz moslamalarni birlashtirish uchun sizning qator qator qatorlaringizdan o'tishingiz mumkin:

var flat = [ ];
for(var i = 0; i < myarr.length; ++i)
    for(var j = 0; j < myarr[i].length; ++j)
        flat[j] = merge(flat[j], myarr[i][j]);

Va keyin tartiblashtirish:

flat.sort(function(a, b) {
    a = a.location;
    b = b.location;
    if(a < b)
        return -1;
    if(a > b)
        return 1;
    return 0;
});

Demo (run with your JavaScript console open): http://jsfiddle.net/ambiguous/twpUF/

Manbalar:

2
qo'shib qo'ydi
katta rahmat
qo'shib qo'ydi muallif learner, manba
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