0

I have a number of variables with values:

apples_count = 1;
oranges_count = 4;
bananas_count = 2;

I want to store them in an array (eg fruits = []) so I can order them by value, ie

[oranges_count, bananas_count, apples_count]

If I attempt to add the variable oranges_count using

fruits.push(oranges_count);

Then the variable value 4 is added but not the variable itself.

How can I store the variables in an array so I can sort them by value?

4
  • 1
    JavaScript doesn't have variable references. You can only store values. Commented Jan 23, 2020 at 7:47
  • 1
    Why can't you sort the array by value when you store the values in the array? What are you really trying to accomplish? Commented Jan 23, 2020 at 7:48
  • +1 thanks for your suggestion - I'm aiming to order the placement of page elements but I found it strange I couldn't do this in an intuitive way Commented Jan 23, 2020 at 7:51
  • 1
    JavaScript is always pass-by-value, so you can't sort "vairables", only their values. If you need to associate some label with a value, you can use an object or a Map or even an array of pairs or objects that will have key-value relationship. Commented Jan 23, 2020 at 7:51

3 Answers 3

3

Don't use separate variables, use an array of objects.

const fruits = [
  {type: "apple", count: 1},
  {type: "orange", count: 4},
  {type: "banana", count: 2}
];

fruits.sort((a, b) => a.count - b.count);
console.log(fruits);

Sign up to request clarification or add additional context in comments.

Comments

1

You have 3 standalone variables with similar functionality. Consider using an object or an array instead, stored in a single variable. Then you can use array methods on the array to sort and retrieve the ordered counts, eg:

const counts = {
  apples: 1,
  oranges: 4,
  bananas: 2,
};
const sortedEntries = Object.entries(counts).sort((a, b) => a[1] - b[1]);
for (const entry of sortedEntries) {
  console.log(entry);
}

With the above, to change a particular count, just do something like counts.apples = 5.

Or, using the array method from the beginning:

const fruits = [
  { name: 'apple', count: 1 },
  { name: 'orange', count: 4 },
  { name: 'banana', count: 2 },
];
const sortedFruits = fruits.sort((a, b) => a.count - b.count);
for (const fruit of sortedFruits) {
  console.log(fruit);
}

To change the count of an object in the above array, either .find the object with the name you want and assign to its count property:

fruits.find(({ name }) => name === 'apple').count = 10;

Or create an object like above, mapping each fruit name to its object.

Comments

1

Store an object instead of only the value. For example, you could use something like this:

var apples = {
  amount: 1,
  name: 'apple',
  type: 'fruit'
};
var oranges = {
  amount: 4,
  name: 'orange',
  type: 'fruit'
};
var bananas = {
  amount: 2,
  name: 'banana',
  type: 'fruit'
};
var collection = [
  apples,
  oranges,
  bananas
];
var sorted_collection = collection.sort(( first, second ) => first.amount - second.amount );

console.log( sorted_collection );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.