在JavaScript中使用MAP()时,在元素上使用字符串函数?

发布于 2025-02-12 10:39:48 字数 571 浏览 1 评论 0 原文

我正在尝试执行以下操作。

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.sort(), s]);

本质上,我要做的是创建一个新的数组数组,其中这个新数组就像 [从第一个数组中排序的字符串,第一个数组中的原始字符串]

但是,它看起来像 .sort()方法在这里无效。

我什至尝试使其

let sorted_str = strs.map((s) => [s.toString().sort(), s]);

强制串起字符串,并确保它具有 sort()方法,但无济于事。

错误是 typeError:s.tostring(...)。排序不是函数

无论如何,我都可以将其工作或任何简单的解决方法。

I am trying to do the following.

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.sort(), s]);

Essentially, what I am trying to do is create a new array of arrays, where this new array is like [sorted string from first array, original string from first array]

However, it looks like the .sort() method is not valid here.

I even tried making it

let sorted_str = strs.map((s) => [s.toString().sort(), s]);

to force a String and make sure it has a sort() method possible, but to no avail.

The error is TypeError: s.toString(...).sort is not a function.

Any way I can get this to work or any simple workaround will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疯到世界奔溃 2025-02-19 10:39:48

您需要获得一系列字符,对其进行排序并恢复字符串。

const
    strs = ["one", "two"],
    sorted_str = strs.map((s) => [Array.from(s).sort().join(''), s]);

console.log(sorted_str);

You need to get an array of characters, sort it and get a string back.

const
    strs = ["one", "two"],
    sorted_str = strs.map((s) => [Array.from(s).sort().join(''), s]);

console.log(sorted_str);

短叹 2025-02-19 10:39:48

您需要转换为数组,然后应用排序方法,然后再次加入字符串,尝试以下操作:

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.split('').sort().join(''), s])

console.log(sorted_str);

You need to convert to array, then apply the sort method, and then join to a string again, try this:

strs = ["one", "two"];
let sorted_str = strs.map((s) => [s.split('').sort().join(''), s])

console.log(sorted_str);

偷得浮生 2025-02-19 10:39:48

而不是使用 split()请勿使用split()

strs = ["one", "two"];
let sorted_str = strs.map((s) => [[...s].sort().join(''), s]);

console.log(sorted_str)

instead of using split(). do not use split()

strs = ["one", "two"];
let sorted_str = strs.map((s) => [[...s].sort().join(''), s]);

console.log(sorted_str)

心的位置 2025-02-19 10:39:48

字符串类没有 .sort()方法。阵列类会做!因此,您可以将字符串转换为数组,对其进行排序,然后将排序的数组转换回字符串。看起来像

s.split("").sort().join("")

The String class does not have a .sort() method. The array class does, though! So you can convert the string to an array, sort that, and then convert the sorted array back to a string. That would look something like

s.split("").sort().join("")
鯉魚旗 2025-02-19 10:39:48

sort()方法需要一个数组作为输入,但是如果您在回调函数中调用它传递给 map(),则在单个字符串上调用它。

我不是100%确定我了解所需的输出,无论如何这是我的解决方案。

让我们从要分类的一系列字符串开始。

const items = ['cat', 'dog', 'elephant', 'bee', 'ant'];

现在,为了对其进行排序而无需突变,我们可以做:

const sortedItems = [...items].sort();

现在,我们有两个阵列,一个原始的阵列,另一个带有排序字符串。

我们可以使用 map()在原始数组上循环并创建所需的数组。 索引回调函数中的参数可用于检索排序的数组中的项目。

const newItems = items.map((item, index) => {
  return [sortedItems[index], item];
});

现在,如果我正确理解我们需要您所需的内容:

[
  ['ant', 'cat'], // 'ant' (from the sorted array), 'cat' (from the original)
  ['bee', 'dog'],  // same thing
  ['cat', 'elephant'], // etc.
  ['dog', 'bee'],
  ['elephant', 'ant']
]

如果您愿意,您可以将所有内容放在一个函数中:

const sortingMethod = (items) => {
  const sortedItems = [...items].sort();
  return items.map((item, index) => {
    return [sortedItems[index], item];
  });
}

您可以这样称呼:

const newElements = sortingMethod(['cat', 'dog', 'elephant', 'bee', 'ant']);
console.log(newElements);

The sort() method needs an array as input, but if you call it in the callback function passed to a map() you are calling it on a single string.

I'm not 100% sure that I understood the desired output, anyway here is my solution.

Let's start with an array of Strings that we want to sort.

const items = ['cat', 'dog', 'elephant', 'bee', 'ant'];

Now in order to sort it without mutating it we can do:

const sortedItems = [...items].sort();

Now we have two arrays, the original one, and another one with the sorted strings.

We can use map() to loop over the original array and to create the array that you need. The index parameter in the callback function can be used to retrieve the item in the sorted array.

const newItems = items.map((item, index) => {
  return [sortedItems[index], item];
});

Now if I understood correctly we have what you needed:

[
  ['ant', 'cat'], // 'ant' (from the sorted array), 'cat' (from the original)
  ['bee', 'dog'],  // same thing
  ['cat', 'elephant'], // etc.
  ['dog', 'bee'],
  ['elephant', 'ant']
]

If you want you can put everything together in one function:

const sortingMethod = (items) => {
  const sortedItems = [...items].sort();
  return items.map((item, index) => {
    return [sortedItems[index], item];
  });
}

That you can call like this:

const newElements = sortingMethod(['cat', 'dog', 'elephant', 'bee', 'ant']);
console.log(newElements);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文