Reputation: 1047
I'm using the reverse() javascript function to reverse the objects held in a local storage array. But I'm trying to marry up the original index with the newly ordered array.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.indexOf("Apple"); //2
var fruitsReversed = fruits.reverse();
fruitsReversed.indexOf("Apple"); //1 -> I want to know that it used to be 2
Is there an easy way to do this? My actual case is a bit more complicated because the original string is in JSON format:
var data = {"info":[{"name":"Max"},{"name":"Alex},{"name":"Sam"},{"name":"Chris"}]}
...but I don't know if that would make a difference.
Update: I've got this working using using a bit of a work-around:
var fruitsLength = fruitsReversed.length-1; //3. subtract one because the first index is 0, not 1
var fruitIndex = fruitsLength - fruitsReversed.indexOf("Apple"); //2 (3-1)
So while this works, I think there must be a cleaner way...
Upvotes: 2
Views: 3891
Reputation: 614
Return the index after substracting the length return fruits.length -1-fruitsReversed.indexOf("Apple")
Upvotes: 3
Reputation: 1727
myArray=[{"name":"Max"},{"name":"Alex"},{"name":"Sam"},{"name":"Chris"}];
pos = myArray.map(function(e) { return e.name; }).reverse().indexOf('Alex');
console.log("position before reverse="+pos);
Upvotes: 0
Reputation: 33161
Firstly in your example fruits === fruitsReversed
. I explain below about how reverse()
happens in place.
You mentioned in the comments that you need to display a reversed version of your array, except you want to edit the original. If your array contains sets of objects, then you don't need to worry about indexes at all. Consider this array:
var arr = [{ val: 1 }, { val: 2 }];
If you reverse this array, it will still contain the original objects, as only the reference is what changes. This means that if you only ever need to alter the array properties (eg. val
in this case), just pass around references to the actual objects. For example:
var arr = [{ val: 1 }, { val: 2 }];
var anObject = arr[0];
arr.reverse();
// this changes the original objects value
anObject.val = 5;
// this prints 5
console.log(arr[1].val);
With this in mind, in your view, simply pass in the index of the item in the reversed array. Personally though, I would use a data binding library like angular or react, and just alter the items themselves, decoupling them from any array ordering.
The docs also explain how reverse()
changes in place.
The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.
What this means is that the actual original array is altered, it isn't copied prior to reversing. You don't have to use what is returned, and can simply call arr.reverse()
.
Upvotes: 0
Reputation: 386620
I suggest to use an unique identifier in an object to access the object without need to know the index of the array.
Then you could reorder the array and the access via hash table is granted.
var data = { info: [{ name: "Max" }, { name: "Alex" }, { name: "Sam" },{ name: "Chris" }] },
hash = Object.create(null);
data.info.forEach(function (a) {
hash[a.name] = a;
});
console.log(hash.Max);
console.log(hash["Sam"]);
console.log(hash);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 0