为了账号安全,请及时绑定邮箱和手机立即绑定

我如何比较两者以正确排序?

我如何比较两者以正确排序?

莫回无 2021-05-03 12:44:07
目的是区分这两个数组,然后控制台按价格范围按顺序记录产品数组,我尝试使用嵌套的for循环来查看是否可以解决问题,但并没有按正确的方向进行,我还在想关于使用破折号。我如何能够比较两个ID之间的ID并按价格顺序推它们?/* Your task is to write a function or set of functions that console logs out the products ordered by price*/var products = [  { id: 1, name: 'Buzzy' },  { id: 2, name: 'Bytrex' },  { id: 3, name: 'CactiDance' },  { id: 4, name: 'CactiLoops' },  { id: 5, name: 'Cadaver Jelly' },  { id: 6, name: 'Caffeine Serene' },  { id: 7, name: 'Cajun Sation' },  { id: 8, name: 'Call it Green' },  { id: 9, name: 'Callflex' },  { id: 10, name: 'Calling Card Shark' },  { id: 11, name: 'Calque' },  { id: 12, name: 'Camel Meal Tea' },  { id: 13, name: 'Camelot Chamomile' },  { id: 14, name: 'Campxotica' },  { id: 15, name: 'Camus the Killer Tale' },  { id: 16, name: 'Candecor' },  { id: 17, name: 'Candelarium' },  { id: 18, name: 'CandID' },  { id: 19, name: 'Candlelight Vittles' },  { id: 20, name: 'Candy Ask' },  { id: 21, name: 'Candy Floss' },];var prices = [  { id: 6, price: 55 },  { id: 14, price: 22 },  { id: 15, price: 57 },  { id: 4, price: 41 },  { id: 18, price: 9 },  { id: 17, price: 3 },  { id: 2, price: 73 },  { id: 7, price: 43 },  { id: 5, price: 78 },  { id: 1, price: 91 },  { id: 8, price: 58 },  { id: 16, price: 69 },  { id: 13, price: 74 },  { id: 19, price: 14 },  { id: 21, price: 25 },  { id: 12, price: 84 },  { id: 20, price: 8 },  { id: 9, price: 94 },  { id: 10, price: 36 },  { id: 3, price: 34 },  { id: 11, price: 71 },];
查看完整描述

2 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

您可以先sort使用prices数组。然后遍历它,并使用如下代码获取products每个:idfind


var products = [{id:1,name:"Buzzy"},{id:2,name:"Bytrex"},{id:3,name:"CactiDance"},{id:4,name:"CactiLoops"},{id:5,name:"Cadaver Jelly"},{id:6,name:"Caffeine Serene"},{id:7,name:"Cajun Sation"},{id:8,name:"Call it Green"},{id:9,name:"Callflex"},{id:10,name:"Calling Card Shark"},{id:11,name:"Calque"},{id:12,name:"Camel Meal Tea"},{id:13,name:"Camelot Chamomile"},{id:14,name:"Campxotica"},{id:15,name:"Camus the Killer Tale"},{id:16,name:"Candecor"},{id:17,name:"Candelarium"},{id:18,name:"CandID"},{id:19,name:"Candlelight Vittles"},{id:20,name:"Candy Ask"},{id:21,name:"Candy Floss"}],

   prices = [{id:6,price:55},{id:14,price:22},{id:15,price:57},{id:4,price:41},{id:18,price:9},{id:17,price:3},{id:2,price:73},{id:7,price:43},{id:5,price:78},{id:1,price:91},{id:8,price:58},{id:16,price:69},{id:13,price:74},{id:19,price:14},{id:21,price:25},{id:12,price:84},{id:20,price:8},{id:9,price:94},{id:10,price:36},{id:3,price:34},{id:11,price:71}];


prices.sort((a, b) => a.price - b.price)

      .forEach(p => {

        console.log(products.find(a => a.id === p.id))

      })


查看完整回答
反对 回复 2021-05-13
  • 2 回答
  • 0 关注
  • 128 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信