简体   繁体   English

Reverse() 在带有字符串键的数组中不起作用

[英]Reverse() doesn't work in array with string key

Why reverse() doesn't work if my array have elements with string as a key.如果我的数组有以字符串为键的元素,为什么reverse()不起作用。

var myArray = [];
myArray["test"] = 100;
myArray["test2"] = 200;

console.log(myArray)
console.log(myArray.reverse())

Both returns the same result.两者都返回相同的结果。

How can I change it to make it work?我怎样才能改变它以使其工作?

DEMO: https://www.w3schools.com/code/tryit.asp?filename=GG4PXCHZ4VUD演示: https://www.w3schools.com/code/tryit.asp?filename=GG4PXCHZ4VUD

.reverse() is a function of arrays, where elements are indexed by their position. .reverse()是 arrays 的 function,其中元素由其 position 索引。 Your code is not adding elements to the array, but rather adding properties on the array object.您的代码不是向数组添加元素,而是在数组 object 上添加属性。 This works and the properties can be accessed, but reversing does nothing as these are not elements.这可行并且可以访问属性,但是由于这些不是元素,因此反转什么也不做。 The array is still of 0 length.数组的长度仍然为 0。

You will have to either:您将不得不:

  1. Make myArray an object of a different type.使 myArray 成为不同类型的 object。 In this case, reverse will still not work, and you will have to write code to sort manually.在这种情况下,反向仍然不起作用,您将不得不编写代码来手动排序。 Other answers have provided some guidance as to how to achieve this其他答案为如何实现这一目标提供了一些指导
  2. Add your elements to the array using push() or numeric indices, in which case you'll lose the string indices but can use array sorting methods such as .reverse()使用push()或数字索引将元素添加到数组中,在这种情况下,您将丢失字符串索引,但可以使用数组排序方法,例如.reverse()

While numbers have an intrinsic ordering, object property keys follow different rules .虽然数字具有内在顺序,但 object 属性键遵循不同的规则 If you want to reverse a string-indexed object, consider writing a function to insert objects to a new Map in reverse order.如果要反转字符串索引的 object,请考虑编写 function 以将对象以相反的顺序插入新的 Map。

Since you do not actually need any array methods, it would be better to simply use an object instead.由于您实际上不需要任何数组方法,因此最好简单地使用 object 代替。

You can create a new reversed array by looping over Object.keys() backwards.您可以通过向后循环Object.keys()来创建新的反向数组。 See the code in action here .请参阅 此处的实际代码。

 var myArray = []; myArray["test"] = 100; myArray["test2"] = 200; function reverseAssociative(arr){ const keys = Object.keys(arr); const res = []; for(let i = keys.length - 1; i >= 0; i--){ res[keys[i]] = arr[keys[i]]; } return res; } const res = reverseAssociative(myArray); for(const key in res){ console.log(key, res[key]); }

You can simplify it by using reduce on Object.entries after reversing.您可以通过在Object.entries上使用reduce来简化它。 See the code in action here .请参阅 此处的实际代码。

myArray = Object.entries(myArray).reverse().reduce((acc,[key,val])=>(acc[key]=val,acc),[])

Arrays values are only accessible by index. Arrays 值只能通过索引访问。

myArray[0] = 100;

myArray[1] = 200;

console.log(myArray) // [100, 200]

console.log(myArray.reverse()) // [200, 100]

You can store values by keys in objects.您可以通过键在对象中存储值。

var myObject = {};
myObject["test"] = 100;
myObject["test2"] = 200;

console.log(myObject) // {test: 100, test2: 200}

That said, you can reverse an object by doing this:也就是说,您可以通过执行以下操作来反转 object:

Object.entries(myObject).reverse().reduce((a, b) => {
 a[b[0]] = b[1];
 return a
},{})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM