Javascript JS中按第一个参数排序数组
我试图在数据被循环和连接后组织数据 已编辑:显示末端零件的实现Javascript JS中按第一个参数排序数组,javascript,arrays,sorting,Javascript,Arrays,Sorting,我试图在数据被循环和连接后组织数据 已编辑:显示末端零件的实现 const request = require('request'); const options = { url: 'https://stockx.com/api/browse?&_search=$jordan 3 unc', method: 'GET', headers: { 'Accept': 'application/json', 'Accept-Cha
const request = require('request');
const options = {
url: 'https://stockx.com/api/browse?&_search=$jordan 3 unc',
method: 'GET',
headers: {
'Accept': 'application/json',
'Accept-Charset': 'utf-8',
'User-Agent': 'stockx'
}
};
request(options, function(err, res, data) {
const json = JSON.parse(data);
console.log(json.Products[0].shoe)
console.log(json.Products[1].media.imageUrl)
for (var n = 1; n < 25; n++){
/* ^ loop through array and then reorganize and join them below */
const sizes = [json.Products[n].market.lastSaleSize];
const sales = [json.Products[n].market.lastSale];
const join = sizes.concat(sales);
v
const request=require('request');
常量选项={
网址:'https://stockx.com/api/browse?&_search=$3 unc',
方法:“GET”,
标题:{
“接受”:“应用程序/json”,
“接受字符集”:“utf-8”,
“用户代理”:“stockx”
}
};
请求(选项、功能(错误、恢复、数据){
const json=json.parse(数据);
console.log(json.Products[0].shoe)
console.log(json.Products[1].media.imageUrl)
对于(变量n=1;n<25;n++){
/*^在数组中循环,然后在下面重新组织并加入它们*/
const size=[json.Products[n].market.lastSaleSize];
const sales=[json.Products[n].market.lastSale];
const join=大小。concat(销售);
v
问题是返回了[size:sale],但大小都不符合顺序,是否有sort()函数可以按数字顺序重新排列大小,但保持连接的销售价格?默认情况下,排序函数将值作为字符串进行比较。要对[price:sale]进行排序正确地说,在进行比较之前,您需要将价格转换为一个数字。然后,您的问题的排序函数如下所示:
yourResult.sort((tupleA, tupleB) => Number(tupleA[0]) - Number(tupleB[0]))
代码的问题是,您使用的数据结构是一个包含两个元素的数组。这些元素只能通过索引访问。arr[0]
或arr[1]
请回答以下问题: 我认为你改变了数据结构,因为原来的文章。是的,值是放在一个单一的数组时,使用concat
您目前的解决方案似乎是复制粘贴的混合。让我向您解释如何解决此问题:
map
功能实现。对于收到的每个产品,它都会返回一个新对象:
const sales = filteredProducts.map(function(product) {
return {
lastSaleSize: product.market.lastSaleSize,
lastSale: product.market.lastSale,
}
})
有关贴图功能的详细信息:
lastSaleSize
和lastSale
。因此可以使用点运算符(.lastSaleSize
)访问lastSaleSize。由于lastSaleSize仍然是一个字符串,因此需要将其转换为一个数字给你的解决方案是最简单的方法
array.sort(function(a,b){
return parseFloat(a[0]) - parseFloat(b[0])
})
但是,我创建了一个函数,该函数将按数组主数组中每个数组的第一个值对数组进行排序
function sort_array_by_first_value(array){
sorted_array = [] //the new array where will will store the sorted values
while(array.length > 0){
//getting an array with the first value in every single of the arrays (eg : [6, 6.5, 5.5])
array_of_first_values = array.map(obj=>parseFloat(obj[0]))
//getting the index of the biggest value
highest_value_index = array_of_first_values.indexOf(Math.max.apply(Math,array_of_first_values))
//pushing the array with the highest value in the first position to our new array, sorted_array
sorted_array.push(array[highest_value_index])
//taking away from original array the array pushed to the new one
array.splice(highest_value_index,1)
}
console.log(sorted_array)
return sorted_array
}
var array =
[[ '6', 235 ],
[ '6.5', 240 ],
[ '5.5', 217 ],
[ '9.5', 249 ],
[ '5', 216 ],
[ '8.5', 230 ],
[ '7', 245 ],
[ '11', 277 ],
[ '4', 210 ],
[ '9', 238 ],
[ '10', 238 ]]
sort_array_by_first_value(array)
我希望你能发现这个有用的@Barmar是的,我试过了,无法理解你的尝试,我们会帮你解决。我们不会为你写的。好的,听起来不错,我会很快将它粘贴到这里,谢谢,这是因为我将这两个元素连接在一起吗?当运行循环时,值会在连接之前分别返回用concat
6 325 8.5 327实现这些功能确实有效,非常感谢David花时间解释,我是JS(这里的图形/网页设计师)的新手在隔离期间尝试学习新东西,不幸的是,这是由于观看教程时出现大量复制和粘贴错误造成的。虽然我已经阅读了大量文档,但需要立即了解的内容很多!谢谢您,它在您的示例中确实有效,但如果我将其插入函数数组=[join](join=[size,sale],它将返回此[[ '10', 235 ] ] [ [ '9', 242 ] ] [ [ '9.5', 243 ] ] [ [ '11', 259 ] ] [ [ '10.5', 255 ] ] [ [ '12', 269 ] ] [ [ '8', 234 ] ] [ [ '8.5', 239 ] ] [ [ '11.5', 282 ] ] [ [ '13', 278 ] ] [ [ '7', 278 ] ] [ [ '7.5', 259 ] ] [ [ '14', 285 ] ] [ [ '12.5', 271 ] ] [ [ '15', 260 ] ] [ [ '16', 250 ] ] [ [ '17', 201 ] ] [ [ '18', 240 ] ] [ [ '13', 950 ] ] [ ['12',3500].['9',4396].[['9.5',2800].[['10',3500].['12.5',2850].
您是如何使用该功能的?我已经调整了主要问题,包括我是如何使用您的功能的,谢谢。
const sales = filteredProducts.map(function(product) {
return {
lastSaleSize: product.market.lastSaleSize,
lastSale: product.market.lastSale,
}
})
const result = sales.sort(function(saleA, saleB) {
return Number(saleA.lastSaleSize) - Number(saleB.lastSaleSize)
})
array.sort(function(a,b){
return parseFloat(a[0]) - parseFloat(b[0])
})
function sort_array_by_first_value(array){
sorted_array = [] //the new array where will will store the sorted values
while(array.length > 0){
//getting an array with the first value in every single of the arrays (eg : [6, 6.5, 5.5])
array_of_first_values = array.map(obj=>parseFloat(obj[0]))
//getting the index of the biggest value
highest_value_index = array_of_first_values.indexOf(Math.max.apply(Math,array_of_first_values))
//pushing the array with the highest value in the first position to our new array, sorted_array
sorted_array.push(array[highest_value_index])
//taking away from original array the array pushed to the new one
array.splice(highest_value_index,1)
}
console.log(sorted_array)
return sorted_array
}
var array =
[[ '6', 235 ],
[ '6.5', 240 ],
[ '5.5', 217 ],
[ '9.5', 249 ],
[ '5', 216 ],
[ '8.5', 230 ],
[ '7', 245 ],
[ '11', 277 ],
[ '4', 210 ],
[ '9', 238 ],
[ '10', 238 ]]
sort_array_by_first_value(array)