0

Following How can I add a key/value pair to a JavaScript object? and Best way to store a key=>value array in JavaScript?, I built my Key/Value map (object).

And I build it like below where MyFunc returns an array:

let MyMap = {}; // object
for(let i = 0; i < MyContainer.length; i++)
{
   // create the key (id of current element) and set its value as array of elements (result from MyFunc)
   await MyFunc(MyContainer[i]).then((response) => MyMap[MyContainer[i].id] = response); 
}

MyMap look like this:

MyMap = 
{
   1: [Object, Object, Object, …] // 13 elements
   2: [Object, Object, Object, …] // 7 elements
   3: [Object, Object, Object, …] // 4 elements
   4: [Object]
   5: [Object]
   6: [Object, Object, Object, …] // 5 elements
   7: [Object, Object, Object, …] // 9 elements
}

I would like to iterate my map (keys) but starting from the key that has the smallest value (The smallest array).

Therefore, I would like to:

  • access MyContainer and pick the element that has id 4, and do stuff (has the smallest array: 1 element)
  • then access MyContainer and pick the element that has id 5, and do stuff
  • then 3
  • then 6
  • .. etc
  • and finally access MyContainer and pick the element that has id 1 and do stuff (has the largest array: 13 elements)

Now since myMap is an object, then I can't sort it.

How can I accomplish this?

3
  • 1
    Well yes, use an array instead. Commented Jun 8, 2017 at 21:41
  • A Map has a keys() function, just sort them and iterate them. Commented Jun 8, 2017 at 21:44
  • @FrederikHansen It's not a Map, he's just calling a regular object literal myMap Commented Jun 8, 2017 at 21:46

1 Answer 1

2

Eventually, you won't be able to use an Object because the order of the keys is not guaranteed.

You can:

  • Use Object.keys(obj) to get the keys of your object as an Array
  • Use those keys to build an Array of objects of your structure, each item in the array being the key/value pair of your original object.
  • Then sort that Arrayof objects based on the length of each item.value within it. The item.value in this case is your original object's values which are Arrays

const obj = {
  "1": [1, 2, 3],
  "2": [1],
  "3": [1, 2],
  "4": [1, 2, 3, 4]
};

// Transform object into a nested array
// Each array item contains the value of a key of your original object,
// which itself is an Array
const arr = Object.keys(obj).reduce((arr, key) => {
 arr.push({ key: key, value: obj[key] });
 
 return arr;
}, [])

// Sort the nested Array based on the length of each item
const sortedArr = arr.sort((a, b) => a.value.length - b.value.length);

console.log(sortedArr);

Same as above but more compact, using chaining

const obj = {
  "1": [1, 2, 3],
  "2": [1],
  "3": [1, 2],
  "4": [1, 2, 3, 4]
};

const sortedArr = Object.keys(obj).reduce((arr, key) => {
  arr.push({ key: key, value: obj[key] });

  return arr;
}, [])
.sort((a, b) => a.value.length - b.value.length)

console.log(sortedArr);

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

11 Comments

I see, but I am still not sure how to implement it. Can you provide a code for my OP? I have properties as IDs, where every ID has an array of elements as a value. I want to iterate the properties by the smallest array first (smallest length)
You need to show us an example of the current data structure you have in code
Sorry but this is still not clear for me. I edited my OP with my current data structure and the desired process that I would like to have
But then how to keep trace of which row to which property (ID)? currently I have property(can be any integer) -> value (array), if I convert to a 2d array then I will have index(starts from 0) -> value (row of elements). Hence I lose trace of the any integer
yes but I also want to properties.. I want to start with the smallest array, true, but at the same time I want to know for which ID this array belongs to (In my OP, see where did I get ID from? which turned to be properties, now I want to go to MyContainer and pick the element that has the smallest array, then the next, and the next, till I finish it all).
|

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.