JSON ma'lumotlarini jildga javonaga aylantiring

Mening json ma'lumotlarim shunday:

[{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"},
{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"2UNHFBCQG0"},
{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"J37612ZVEC"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"OB60BC33LF"},
{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"DCA3364SGK"},
{"vehicleType":"car","make":"Volvo","model":"XC60","vin":"FV2HML056G"},
{"vehicleType":"car","make":"BMW","model":"328xi","vin":"IGPP3DQ4SK"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}]

Endi men qatorni JavaScript-da shunday bo'lishini xohlayman:

// Javascript array
var array = [];
array[// vin number od above json array] =//the whole row of that vin number.

Misol uchun.,

// Javascript array
array["R90N8E95UB"][0] 
=[{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}]
array["R90N8E95UB"][1] 
=[{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}]

Va hokazo...

Iltimos, bu haqda maslahat bering.

Rahmat.

2

9 javoblar

Yineleme va ichki qatorga suring.

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

var data = [{ vehicleType: "car", make: "Dodge", model: "Durango", vin: "R90N8E95UB" }, { vehicleType: "car", make: "Honda", model: "Accord", vin: "XJLSXU6NA5" }, { vehicleType: "car", make: "Dodge", model: "Durango", vin: "2UNHFBCQG0" }, { vehicleType: "car", make: "Volvo", model: "XC90", vin: "J37612ZVEC" }, { vehicleType: "car", make: "Mercedes", model: "C250", vin: "J5QB24CFGS" }, { vehicleType: "car", make: "Dodge", model: "Durango", vin: "OB60BC33LF" }, { vehicleType: "car", make: "Volvo", model: "XC90", vin: "DCA3364SGK" }, { vehicleType: "car", make: "Volvo", model: "XC60", vin: "FV2HML056G" }, { vehicleType: "car", make: "BMW", model: "328xi", vin: "IGPP3DQ4SK" }, { vehicleType: "car", make: "Mercedes", model: "C250", vin: "J5QB24CFGS" }, { vehicleType: "car", make: "Honda", model: "Accord", vin: "XJLSXU6NA5" }, { vehicleType: "car", make: "Mercedes", model: "C250", vin: "J5QB24CFGS" }, { vehicleType: "car", make: "Dodge", model: "Durango", vin: "R90N8E95UB" }],
    result = Object.create(null),
    keys;

data.forEach(function (a) {
    result[a.vin] = result[a.vin] || [];
    result[a.vin].push(a);
});

keys = Object.keys(result);

console.log(keys);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
</div> </div>
8
qo'shib qo'ydi
Xristian @Nina, shuningdek, bu noyob vinaning barcha kalitlarini xohlayman. Menga xuddi shunday yordam bera olasizmi? Misol uchun, bor key = ["R90N8E95UB", "XJLSXU6NA5", "2UNHFBCQG0", ...]
qo'shib qo'ydi muallif Chetan Nakum, manba
Bu ajoyib @NinaScholz bo'lar edi. Yordam uchun juda minnatdormiz.
qo'shib qo'ydi muallif Chetan Nakum, manba
@ChetanNakum, iltimos, tahrir qiling.
qo'shib qo'ydi muallif Nina Scholz, manba

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

var json = [{vehicleType:"car",make:"Dodge",model:"Durango",vin:"R90N8E95UB"},{vehicleType:"car",make:"Honda",model:"Accord",vin:"XJLSXU6NA5"},{vehicleType:"car",make:"Dodge",model:"Durango",vin:"2UNHFBCQG0"},{vehicleType:"car",make:"Volvo",model:"XC90",vin:"J37612ZVEC"},{vehicleType:"car",make:"Mercedes",model:"C250",vin:"J5QB24CFGS"},{vehicleType:"car",make:"Dodge",model:"Durango",vin:"OB60BC33LF"},{vehicleType:"car",make:"Volvo",model:"XC90",vin:"DCA3364SGK"},{vehicleType:"car",make:"Volvo",model:"XC60",vin:"FV2HML056G"},{vehicleType:"car",make:"BMW",model:"328xi",vin:"IGPP3DQ4SK"},{vehicleType:"car",make:"Mercedes",model:"C250",vin:"J5QB24CFGS"},{vehicleType:"car",make:"Honda",model:"Accord",vin:"XJLSXU6NA5"},{vehicleType:"car",make:"Mercedes",model:"C250",vin:"J5QB24CFGS"},{vehicleType:"car",make:"Dodge",model:"Durango",vin:"R90N8E95UB"}],
    result = [];
 
    json.forEach(v => result.push([v.vin, v]));
    console.log(result);
</div> </div>
5
qo'shib qo'ydi
Sizning "boshqa savol" ga javobingizni yo'qotmagan bo'lsangiz - bu boshqacha edi va u ishladi - va uni qimmatli qildi. SO ga qarshi kurashish muammosiga kelsak, buni quyidagicha qo'shishni rejalashtirmoqchi edim: SO ga qarshi tushish demokratiya haqida ko'p gapirilganga o'xshaydi - dunyodagi eng yaxshi boshqaruv shakli ekanligi haqida har kimga (pastga) ovoz berishga ruxsat berish.
qo'shib qo'ydi muallif crashwap, manba

ES2015 da: var arr = ob'ekt.values ​​(obj);

2
qo'shib qo'ydi

Umid qilamanki:

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

var arr = {};
var pop = [{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"},{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"2UNHFBCQG0"},{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"J37612ZVEC"},{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"OB60BC33LF"},{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"DCA3364SGK"},{"vehicleType":"car","make":"Volvo","model":"XC60","vin":"FV2HML056G"},{"vehicleType":"car","make":"BMW","model":"328xi","vin":"IGPP3DQ4SK"},{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}];
pop.forEach(function(item){
  if ( arr[item.vin] ) {   
   arr[item.vin].push([item]);
  } else {
    arr[item.vin] = [[item]];
  }
});
console.log(JSON.stringify(arr["R90N8E95UB"][0]));
console.log(JSON.stringify(arr["R90N8E95UB"][1]));
console.log(JSON.stringify(arr));
</div> </div>
2
qo'shib qo'ydi

You can use Array.reduce

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

var yourData = [{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"},
{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"2UNHFBCQG0"},
{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"J37612ZVEC"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"OB60BC33LF"},
{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"DCA3364SGK"},
{"vehicleType":"car","make":"Volvo","model":"XC60","vin":"FV2HML056G"},
{"vehicleType":"car","make":"BMW","model":"328xi","vin":"IGPP3DQ4SK"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}];


 var result = yourData.reduce(function (acc,curr) {
    acc[curr.vin]= acc[curr.vin] || [];
    acc[curr.vin].push(curr);
    return acc;
},{});
console.log(result);
</div> </div>
1
qo'shib qo'ydi

Nima uchun uni nimaga tayinlamaysiz?

var array =    [{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"},
{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"2UNHFBCQG0"},
{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"J37612ZVEC"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"OB60BC33LF"},
{"vehicleType":"car","make":"Volvo","model":"XC90","vin":"DCA3364SGK"},
{"vehicleType":"car","make":"Volvo","model":"XC60","vin":"FV2HML056G"},
{"vehicleType":"car","make":"BMW","model":"328xi","vin":"IGPP3DQ4SK"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
{"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}]

Keyin

array[0]:
Object {vehicleType: "car", make: "Dodge", model: "Durango", vin: "R90N8E95UB"}

Lekin, agar ularning vinalaridan foydalanib, mashina qidirishni talab qilsangiz, vinni kaliti qiling va o'zgarmaydigan bir qator emas, balki ob'ekt bo'lishi kerak.

var object = {"R90N8E95UB": {"vehicleType": "car", make: "Dodge", model: "Durango"},
              "J5QB24CFGS": {"vehicleType":"car","make":"Mercedes","model":"C250"}}
1
qo'shib qo'ydi

buni sinab ko'ring

var data = [{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"},
        {"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
        {"vehicleType":"car","make":"Dodge","model":"Durango","vin":"2UNHFBCQG0"},
        {"vehicleType":"car","make":"Volvo","model":"XC90","vin":"J37612ZVEC"},
        {"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
        {"vehicleType":"car","make":"Dodge","model":"Durango","vin":"OB60BC33LF"},
        {"vehicleType":"car","make":"Volvo","model":"XC90","vin":"DCA3364SGK"},
        {"vehicleType":"car","make":"Volvo","model":"XC60","vin":"FV2HML056G"},
        {"vehicleType":"car","make":"BMW","model":"328xi","vin":"IGPP3DQ4SK"},
        {"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
        {"vehicleType":"car","make":"Honda","model":"Accord","vin":"XJLSXU6NA5"},
        {"vehicleType":"car","make":"Mercedes","model":"C250","vin":"J5QB24CFGS"},
        {"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}];



        var array = [];
        for(var k= 0 ,l = data.length;k
1
qo'shib qo'ydi

Sizning JSON ma'lumotingiz allaqachon bir qator. Ma'lumotlar olishning bir usuli saytida topish mumkin. Ob'ektlarning qiymatidan yoki xususiyatidan JavaScript ob'ektini olish U erda siz JSON arizasini surish uchun yo'l topa olasiz.

0
qo'shib qo'ydi

Ob'ektga siz ko'rsatgan yo'lingiz bor bo'lsa, siz Object.values ​​() yordamida bir qatorni olishingiz mumkin.

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