从字符串数组中提取价格并按价格排序。

huangapple go评论76阅读模式
英文:

Extract price from string array and sort by price

问题

以下是您要翻译的内容:

"I need help determining the best way to extract the price value out of the strings in the array received from the API, and then reorder the array based on the lowest price values.

These are some of the data that I received from the API.

let luxcar_data = ["Ferrari70", "Bugatti45", "McLaren17", "Lamborghini41", "Rolls52",  "Aston30", "Koenigsegg19", "Pagani28", "Lamborghini36", ...];

The outcome should look like this.

[{name: "McLaren", price: 17},  {name: "Koenigsegg, price: 19}, .... , {name: "Ferrari", price: 70}]"


<details>
<summary>英文:</summary>

I need help determining the best way to extract the price value out of the strings in the array received from the API, and then reorder the array based on the lowest price values.

These are some of the data that I received from the API.

let luxcar_data = ["Ferrari70", "Bugatti45", "McLaren17", "Lamborghini41", "Rolls52", "Aston30", "Koenigsegg19", "Pagani28", "Lamborghini36", ...];


The outcome should look like this.

[{name: "McLaren", price: 17}, {name: "Koenigsegg, price: 19}, .... , {name: "Ferrari", price: 70}]


</details>


# 答案1
**得分**: 2

[`String#match`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/match) 可用于提取名称和价格。 [`Array#map`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map) 可以应用于将数组的每个元素转换为具有名称和价格的对象。然后,直接调用 [`Array#sort`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) 并在比较函数中减去价格。

```javascript
let arr = ["Ferrari70", "Bugatti45", "McLaren17", "Lamborghini41", "Rolls52", "Aston30", "Koenigsegg19", "Pagani28", "Lamborghini36"];
let res = arr.map(s => {
  const [, name, price] = s.match(/([a-z]+)(\d+)/i);
  return {name, price};
}).sort((a, b) => a.price - b.price);
console.log(res);

【注意】:上述是代码部分的翻译。

英文:

String#match can be used to extract the name and price. Array#map can be applied to transform each element of the array to an object with the name and price. Then, directly call Array#sort and substract the prices in the compare function.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let arr = [&quot;Ferrari70&quot;, &quot;Bugatti45&quot;, &quot;McLaren17&quot;, &quot;Lamborghini41&quot;, &quot;Rolls52&quot;,  &quot;Aston30&quot;, &quot;Koenigsegg19&quot;, &quot;Pagani28&quot;, &quot;Lamborghini36&quot;];
let res = arr.map(s =&gt; {
  const [, name, price] = s.match(/([a-z]+)(\d+)/i);
  return {name, price};
}).sort((a, b) =&gt; a.price - b.price);
console.log(res);

<!-- end snippet -->

答案2

得分: 0

代码部分不需要翻译,以下是翻译好的部分:

你可以使用正则表达式来提取 luxcar_data 项中的数字部分和非数字部分。输出是通过传递比较价格的函数进行排序的。

  • /[0-9]/g - 匹配任何数字
  • /\D/g - 匹配任何非数字字符。

希望这对你有所帮助。

英文:

You can use regex to extract digit and non-digit parts of the luxcar_data items. The output is sorted by passing the function that compares the prices.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let luxcar_data = [&quot;Ferrari70&quot;, &quot;Bugatti45&quot;, &quot;McLaren17&quot;, &quot;Lamborghini41&quot;, &quot;Rolls52&quot;,  &quot;Aston30&quot;, &quot;Koenigsegg19&quot;, &quot;Pagani28&quot;, &quot;Lamborghini36&quot;]


let output = luxcar_data.map((car) =&gt; {
  let name = car.replace(/[0-9]/g, &#39;&#39;); 
  let price = parseInt(car.replace(/\D/g,&#39;&#39;)); 
  return { name, price };
}).sort((a, b) =&gt; a.price - b.price);

console.log(output);

<!-- end snippet -->

  • /[0-9]/g - matches any digit
  • /\D/g - matches any non-digit charater.

答案3

得分: 0

以下是您提供的代码的翻译部分:

let res_data = [];

luxcar_data.map((ele) => {
  let patt = /([a-zA-Z]+)(\d+)/g;
  res_data.push({name: ele.replace(patt, "$1"), price: parseInt(ele.replace(patt, "$2")})
})

console.log(res_data.sort((a, b) => a.price - b.price));

请注意,代码中的HTML实体(例如&gt;&quot;)在翻译中被保留,因为它们是代码的一部分,而不是自然语言文本。如果您有任何进一步的疑问或需要帮助,请随时提出。

英文:

My answer:

let res_data = [];

luxcar_data.map((ele)=&gt; {
  let patt = /([a-zA-Z]+)(\d+)/g;
  res_data.push({name:ele.replace(patt, &quot;$1&quot;), price:parseInt(ele.replace(patt, &quot;$2&quot;))})
})

console.log(res_data.sort((a,b)=&gt;a.price-b.price));

I'd appreciate it if you may verify my answer.

huangapple
  • 本文由 发表于 2023年2月24日 14:35:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定