我的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"}]
现在我希望我的数组在JavaScript中像这样:
// Javascript array
var array = [];
array[// vin number od above json array] = // the whole row of that vin number.
例如。,
// Javascript array
array["R90N8E95UB"][0]
=[{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}]
array["R90N8E95UB"][1]
=[{"vehicleType":"car","make":"Dodge","model":"Durango","vin":"R90N8E95UB"}]
等等......
请提供建议。
谢谢。
答案 0 :(得分:8)
您可以迭代并推送到内部数组。
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; }

答案 1 :(得分:5)
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);
答案 2 :(得分:2)
在ES2015中: var arr = Object.values(obj);
答案 3 :(得分:2)
希望这会有所帮助:
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));

答案 4 :(得分:1)
您可以使用Array.reduce
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);
答案 5 :(得分:1)
试试这个
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<l;k++){
array[data[k].vin] = data[k];
}
console.log(array);
答案 6 :(得分:1)
为什么不简单地将它分配给某些东西?
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"}]
然后
array[0]:
Object {vehicleType: "car", make: "Dodge", model: "Durango", vin: "R90N8E95UB"}
但是如果你需要根据他们的vin查找汽车,请将vin作为密钥,变量需要是一个对象,而不是一个数组。
var object = {"R90N8E95UB": {"vehicleType": "car", make: "Dodge", model: "Durango"},
"J5QB24CFGS": {"vehicleType":"car","make":"Mercedes","model":"C250"}}
答案 7 :(得分:0)
如果你有一个对象,你可以通过Object.values()
获得一个数组答案 8 :(得分:0)
您的JSON数据已经是一个数组。可以在Get JavaScript object from array of objects by value or property找到获取数据的一种方法 在那里,您可以找到一种查询JSON数组的方法。