Javascript 包含多个包含多个对象的数组的数组
Javascript Array containing multiple arrays which contain multiple objects
我正在使用来自 Edmunds 汽车 API 的 JSON 数据。这是返回数据的简化版本:
[[
{drivenWheels: "front wheel drive", price: 32442},
{drivenWheels: "front wheel drive", price: 42492},
{drivenWheels: "front wheel drive", price: 38652},
{drivenWheels: "front wheel drive", price: 52402}
],
[{drivenWheels: "all wheel drive", price: 29902},
{drivenWheels: "all wheel drive", price: 34566},
{drivenWheels: "all wheel drive", price: 33451},
{drivenWheels: "all wheel drive", price: 50876}
]
]
在此示例中,有 2 个内部数组代表适用于该车型的不同传动系统选项(前轮和全轮)。
我试图找到每个相应传动系统的最低价格并将对象推入一个新数组。在我的示例中,我希望最终结果是..
var finalArr = [{drivenWheels: "front wheel drive", price: 32442},{drivenWheels: "all wheel drive", price: 29902}]
我已经尝试解决这个问题一段时间了,但一直想不出来。这是我目前所拥有的。
function findLowestPriceDrivenWheels(arr){
var wheelAndPriceArr = [];
var shortest = Number.MAX_VALUE;
for (var i = 0; i < arr.length; i++){
//loops inner array
for (var j = 0; j < arr[i].length; j++){
if(arr[i][j].price < shortest){
shortest = arr[i][j].price;
wheelAndPriceArr.length = 0;
wheelAndPriceArr.push(arr[i][j]);
}
}
}
console.log(wheelAndPriceArr);
return wheelAndPriceArr;
}
如果有1个内部数组。我可以让它工作。问题是当有 2,3 或 4 个内部阵列(代表传动系统)时。我想编写一个函数来处理 API returns 中任意数量的传动系统。
我实际上明白为什么我的解决方案不起作用。问题是我是新手,我遇到了理解障碍。解决方案有点超出我的掌握范围。
这里有 2 个类似的问题很有帮助,但它们涉及 1 个内部数组,但我仍然无法弄明白。任何帮助,将不胜感激!
Here and Here
使用 underscore.js 更容易 (http://underscorejs.org/)
arr = [
[
{drivenWheels: "front wheel drive", price: 32442},
{drivenWheels: "front wheel drive", price: 42492},
{drivenWheels: "front wheel drive", price: 38652},
{drivenWheels: "front wheel drive", price: 52402}
],
[
{drivenWheels: "all wheel drive", price: 29902},
{drivenWheels: "all wheel drive", price: 34566},
{drivenWheels: "all wheel drive", price: 33451},
{drivenWheels: "all wheel drive", price: 50876}
]
]
_.map(arr, function(items){
return _.chain(items)
.sortBy(function(item){
return item.price
})
.first()
.value();
})
假设 arr 是你的 JSON
var results = [];
arr.forEach(function(a,i){a.forEach(function(b){(!results[i]||b['price'] < results[i]['price'])&&(results[i]=b);});});
我非常喜欢单线
结果 (从控制台复制粘贴)
[
{
"drivenWheels":"front wheel drive",
"price":32442
},
{
"drivenWheels":"all wheel drive",
"price":29902
}
]
更加直截了当
var results = [],
i;
for (i = 0; i < arr.length; i += 1) {
var a = arr[i],
j;
for (j = 0; j < a.length; j += 1) {
var b = a[j];
// !results[i] will check if there is a car for the current drivenWheels. It will return true if there isn't
if (!results[i] || b['price'] < results[i]['price']) {
results[i] = b;
}
}
}
我正在使用来自 Edmunds 汽车 API 的 JSON 数据。这是返回数据的简化版本:
[[
{drivenWheels: "front wheel drive", price: 32442},
{drivenWheels: "front wheel drive", price: 42492},
{drivenWheels: "front wheel drive", price: 38652},
{drivenWheels: "front wheel drive", price: 52402}
],
[{drivenWheels: "all wheel drive", price: 29902},
{drivenWheels: "all wheel drive", price: 34566},
{drivenWheels: "all wheel drive", price: 33451},
{drivenWheels: "all wheel drive", price: 50876}
]
]
在此示例中,有 2 个内部数组代表适用于该车型的不同传动系统选项(前轮和全轮)。
我试图找到每个相应传动系统的最低价格并将对象推入一个新数组。在我的示例中,我希望最终结果是..
var finalArr = [{drivenWheels: "front wheel drive", price: 32442},{drivenWheels: "all wheel drive", price: 29902}]
我已经尝试解决这个问题一段时间了,但一直想不出来。这是我目前所拥有的。
function findLowestPriceDrivenWheels(arr){
var wheelAndPriceArr = [];
var shortest = Number.MAX_VALUE;
for (var i = 0; i < arr.length; i++){
//loops inner array
for (var j = 0; j < arr[i].length; j++){
if(arr[i][j].price < shortest){
shortest = arr[i][j].price;
wheelAndPriceArr.length = 0;
wheelAndPriceArr.push(arr[i][j]);
}
}
}
console.log(wheelAndPriceArr);
return wheelAndPriceArr;
}
如果有1个内部数组。我可以让它工作。问题是当有 2,3 或 4 个内部阵列(代表传动系统)时。我想编写一个函数来处理 API returns 中任意数量的传动系统。 我实际上明白为什么我的解决方案不起作用。问题是我是新手,我遇到了理解障碍。解决方案有点超出我的掌握范围。
这里有 2 个类似的问题很有帮助,但它们涉及 1 个内部数组,但我仍然无法弄明白。任何帮助,将不胜感激! Here and Here
使用 underscore.js 更容易 (http://underscorejs.org/)
arr = [
[
{drivenWheels: "front wheel drive", price: 32442},
{drivenWheels: "front wheel drive", price: 42492},
{drivenWheels: "front wheel drive", price: 38652},
{drivenWheels: "front wheel drive", price: 52402}
],
[
{drivenWheels: "all wheel drive", price: 29902},
{drivenWheels: "all wheel drive", price: 34566},
{drivenWheels: "all wheel drive", price: 33451},
{drivenWheels: "all wheel drive", price: 50876}
]
]
_.map(arr, function(items){
return _.chain(items)
.sortBy(function(item){
return item.price
})
.first()
.value();
})
假设 arr 是你的 JSON
var results = [];
arr.forEach(function(a,i){a.forEach(function(b){(!results[i]||b['price'] < results[i]['price'])&&(results[i]=b);});});
我非常喜欢单线
结果 (从控制台复制粘贴)
[
{
"drivenWheels":"front wheel drive",
"price":32442
},
{
"drivenWheels":"all wheel drive",
"price":29902
}
]
更加直截了当
var results = [],
i;
for (i = 0; i < arr.length; i += 1) {
var a = arr[i],
j;
for (j = 0; j < a.length; j += 1) {
var b = a[j];
// !results[i] will check if there is a car for the current drivenWheels. It will return true if there isn't
if (!results[i] || b['price'] < results[i]['price']) {
results[i] = b;
}
}
}